The first question is how are they related? Categories, same channel, other field? There’s a couple approaches to this based on the relationship:
The first one is manual using the relationship field. You would manually relate other entries to the current one when you create the entry.
The more automagical way is to use the {exp:channel:entries}
tag. Then you’d have the option to show random articles, or order them however you’d like. You could even do this in an embed so you can use it wherever you want with different channels.
Example:
{!-- in my entry template --}
{exp:channel:entries channel="news" limit="1"}
<h1>{title}</h1>
{article_content}
<h2>Related Entries: </h2>
{embed="embeds/related_entries" channel_id="{channel_id}"}
{/exp:channel:entries}
{!-- my embed code --}
{exp:channel:entries channel="{emebd:channel_id}" limit="5" orderby="random" dynamic="no"}
<ul>
<li><a href="news/{url_title}">{title}</li>
</ul>
{/exp:channel:entries}
In this method the orderby
parameter is going to be the key to making this dynamic and as relevant as you want: https://docs.expressionengine.com/latest/channels/entries.html#orderby
Thank you. I would like to do this magically by reading the entries in the same channel.
Your second example code does not display any related channel entries but just one result. And that result is the same entry I’m currently viewing. I guess because there is something wrong in the code (not the embed typo). And then it just outputs the title of the current entry.
Random order is fine for now. I guess in the future I can try to do something more fancy like the most viewed one. But for now, I just need to fill up some content.
When I mean the same channel I don’t mean all entries. That would make no sense. I have different articles under different categories. It would make no sense to display a related entry that talks about cars under and entry that talks about dogs.
Maybe there is a way to scan the same text and look for similar keywords? If not, then maybe the best approach is to use the same category. But that would mean the related entries are very limited, not really useful vs just browsing the category directly. Some articles might be related to each other in a different category. If reading the entry text or fields is not possible automagically then I guess my only option is to do it manually.
Ah. I forgot dynamic="no"
parameter. I’ve updated the snippet above.
If you want to add categories, then just add category parameters to the embedded channel:entries tag as well. Sounds like you might want to include a few categories that are related to give the user a variety of options.
Overall you can get pretty creative with how you do this. Maybe some categories should only pull related articles from themselves, while others could pull from multiple categories. In that case you might right a series of conditionals that check if the category is X and then passes to the embed a list of categories to pull from.
If it’s worth it to you, then you can also look at EE Harbor’s Tag add-on. This adds the concept of tags which can be across categories and gives you another dimension to relate articles by.
Thank you, that works now with the dynamic parameter.
The tags add-on is on my planned purchase list as well since I also need to tag articles per keywords in the future. As you said, there seems to be multiple ways to do this, and I will test which approach gets me the best results.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.