Related Entries - Hiranyaloka/Documentation GitHub Wiki
<mt:SetVarBlock name="tags"><mt:EntryTags glue=" OR "><mt:TagName></mt:entrytags></mt:setvarblock> <ul> <mt:Entries tags="$tags"> <li><a href="<mt:EntryPermalink>"><mt:EntryTitle></a></li> </mt:entries> </ul>
In MT4, the above code (used in an entry context) would output a list of entries with the same tags as the current entries. Note that there is no "weighting" happening here, meaning that an entry that has 5 tags in common is not scored higher than an entry with only one tag in common, and entries will be sorted by date, not by "related-ness".
The version below includes a few tweaks, commentary and is formatted for readability:
<mt:Ignore> Compile the current entry's tags into a boolean "OR" statement suitable for use in an mt:Entries "tag" attribute and store it in the related_tags MT variable. Private tags are also being output here in order to ensure all similarly tagged entries are retrieved but they are immediately stored in the variable and not displayed on the blog. </mt:ignore> <mt:SetVarBlock name="related_tags"> <mt:EntryTags include_private="1" glue=" OR "> <mt:Ignore> We use normalize here in case of tags with spaces or other problematic characters </mt:ignore> <mt:TagName normalize="1"> </mt:entrytags> </mt:setvarblock> <mt:Ignore> Here we clean up the value of related_tags in order to get rid of all of the whitespace and make the boolean statement syntactically correct. </mt:ignore> <mt:Var name="related_tags" strip_linefeeds="1" setvar="related_tags"> <mt:SetVarBlock name="list_items"> <mt:Ignore> Here we use our stored boolean statement containing of related tags </mt:ignore> <mt:Entries tags="$related_tags" unique="1"> <mt:Ignore> Skip listing the current entry </mt:ignore> <mt:Unless tag="EntryID" eq="$current_entry"> <li><a href="<mt:EntryPermalink />"><mt:EntryTitle /></a></li> <$mt:Var name="items_found" value="1"$> </mt:unless> </mt:entries> </mt:setvarblock> <mt:If name="items_found"> <h3>Related Blog Entries</h3> <ul> <$mt:Var name="list_items"$> </ul> </mt:if> </mt:entryiftagged>
To use this recipe for Pages, simply convert the following:
- Entries => Pages
- EntryIfTagged => PageIfTagged
- EntryID => PageID
- EntryTags => PageTags
- EntryTitle => PageTitle
- EntryPermalink => PagePermalink