We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

[SOLVED] Related articles

How Do I?

vw000's avatar
vw000
241 posts
4 years ago
vw000's avatar vw000

What would be the best way to show a few related channel entries under a specific channel entry?

Example:

Related Entries:

Shows 4 or 5 results

From the same channel. All entries go in the same channel but I want to show the reader similar articles after reading one.

       
Andy McCormick's avatar
Andy McCormick
183 posts
4 years ago
Andy McCormick's avatar Andy McCormick

The first question is how are they related? Categories, same channel, other field? There’s a couple approaches to this based on the relationship:

  1. The first one is manual using the relationship field. You would manually relate other entries to the current one when you create the entry.

  2. 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

       
vw000's avatar
vw000
241 posts
4 years ago
vw000's avatar vw000

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.

       
Andy McCormick's avatar
Andy McCormick
183 posts
4 years ago
Andy McCormick's avatar Andy McCormick

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.

       
vw000's avatar
vw000
241 posts
4 years ago
vw000's avatar vw000

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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.