Displaying Relationships
Tag is an intuitive relationship tool. It lets you freely apply relevant tags to content on your site. You can then display in your templates the relationships that emerge within this data to improve your visitors' experience. The beauty of Tag relationships is that they happen automatically with very little input on your part. Regular relationship fields require a lot of work searching for entries to relate to, and then constantly updating these entries over time, as they quickly can get outdated over time when new entries are added.
Tag on the other hand, just requires the author to assign a few tags of their choice (or use existing tags that Tag suggests to use, based on the content in the entry's custom fields). Tag does the rest. Using a simple template tag in your page, Tag will automatically display entries that are related to the entry being viewed, based on comparing the sets of tags each entry has. For example, if the main entry you're viewing contains the tags: peanut, acorns, and pants, any other entries that contain one or more of those tags will show. Additionally, these related entries can be ranked by most tag matches. If an entry contains 2 of those tags, it will be ranked higher than entries that contain just 1 match to those tags.
Some examples of usage might be one of the following:
- Have an article display a list of other articles that are similar to it.
- Have a store product display a list of other products that are similar to it.
- Have an article or blog post that displays a multiple sets of related entries:
- other related articles / blog posts.
- products that are likely related to the topic your article or blog post mention.
- a list of clients or sites that use some of the products you mention in the article, but based on specific tagging.
This page will show you how to quickly get started using this feature.
1) Creating a Tag Field & Assigning Tags to Entries
If you have not yet created a Tag field and have not assigned any tags to entries, click here to learn how to do so.
2) Displaying Related Entries
To display other entries with similar tags, you simply write a special template loop using the template 'related_entries':
Note: The demo templates that the addon has 'tag_inc/entry_relationships' is the exact one as below
{exp:tag:related_entries
entry_id="{embed:entry_id}"
orderby="relevance" sort="desc"
limit="5"
dynamic="no"}
{if count == "1"}
<div class="list-group">
<span class="list-group-item list-group-item-info">
<h5>Entries with Similar Tags</h5>
</span>
{/if}
<a href="{url_title_path='tag_main/entry'}" class="list-group-item">
<b>{title}</b> <small class="text-info"><span class="glyphicon glyphicon-tags" aria-hidden="true"></span> {tag_relevance} matches ({tag_relevance_percent}%)</small>
{!-- Begin Testing and Debugging --}
<ul class="solspace-debug">
<li>Count: <kbd>{count}</kbd></li>
<li>Total Results: <kbd>{total_results}</kbd></li>
<li>Absolute Count: <kbd>{absolute_count}</kbd></li>
<li>Absolute Results: <kbd>{absolute_results}</kbd></li>
<li>Embed Entry ID: <kbd>{embed:entry_id}</kbd></li>
</ul>
{!-- End Testing and Debugging --}
</a>
{if tag_no_results}
<div class="alert alert-warning" role="alert">
There are no entries with related tags.
</div>
{/if}
{if count == total_results}
</div>
{/if}
{tag_paginate}
<ul class="pagination pagination-sm">
{tag_pagination_links}
{first_page}
<li><a href="{pagination_url}">First</a></li>
{/first_page}
{previous_page}
<li><a href="{pagination_url}">« Previous</a></li>
{/previous_page}
{page}
<li{if current_page} class="active"{/if}><a href="{pagination_url}">{pagination_page_number}</a></li>
{/page}
{next_page}
<li><a href="{pagination_url}">Next »</a></li>
{/next_page}
{last_page}
<li><a href="{pagination_url}">Last</a></li>
{/last_page}
{/tag_pagination_links}
</ul>
{/tag_paginate}
{/exp:tag:related_entries}
Be sure to specify all necessary and relevant parameters in the Tag:Related_Entries loop.
Now to use this what we can do is pass the entry_id that we are currently viewing in a main entry loop.
Below example assumes that you have the above saved in the template tag_inc/entry_relationships. This can be modified to whatever you need.
Note: The example templates use this same idea in the template 'tag_main/entry'
{exp:channel:entries channel="your_channel" limit="10"}
{title}
{!-- Our main tags for THIS entry --}
{exp:tag:tags entry_id="{entry_id}"}
{tag}
{/exp:tag:tags}
{!-- Our RELATED --}
Related Tags:
{embed="tag_inc/entry_relationships" entry_id="{entry_id}"}
{/exp:channel:entries}
The above code will display the main entry you're viewing, and will load a list of up to 5 other entries that contain similar tags. Because it's ordered by relevance, it will load the top 5 entries with the most tag matches. If there are no other entries that meet the parameter criteria and/or do not contain any of the same tags, then no results will be shown.
That's it!
As you add more entries with tags assigned to them, these related entries lists just update dynamically and always show the latest most up-to-date data. For more information about displaying and handling of related entries by tags.