ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Displaying entries of the same category as "main"-entry

April 22, 2011 8:29pm

Subscribe [3]
  • #1 / Apr 22, 2011 8:29pm

    lehrerfreund

    263 posts

    Hi,

    on my homepage I am showing some entries in a list (title, summary, read-more-link) with the exp:channel:entries-tag.

    Now I want for every entry to show up a list of 3 entries that are from the same category (“this articles could be interesting for you”).

    As far as I can see I would have to nest 2 channel:entries-tags, and this does not work.
    Does anybody know another solution?

    Thanks in advance!!!

    (ps: don’t want to use the “related”-function because when I use it the view-counters don’t work no more)

  • #2 / Apr 22, 2011 10:01pm

    iain

    317 posts

    Whenever you need to nest channel entries tags, go for an embed and pass the parent entry’s category_id(s) to the child template.

    So something in your parent like:

    {exp:channel:entries channel="foo" limit="3"}
    <h1>{title}</h1>
    {summary}
    {embed="inc/.related_articles" category_ids="{categories backspace='1'}{category_id}|{/categories}"}
    ....

    And then in your inc/.related_articles template:

    {if embed:category_ids}
    {exp:channel:entries channel="foo" limit="3" category="{embed:category_ids}"}
    ...
    {/if}

    So essentially category_ids should be something like 12|2|3 for example, and that’ll work directly in the child template’s categories parameter.

  • #3 / Apr 23, 2011 4:11pm

    Greg Salt

    3988 posts

    Hi lehrerfreund,

    How are you getting on? Did Iain’s suggestion help you out?

    Cheers

    Greg

    @Iain, thanks very much for the help.

  • #4 / Apr 23, 2011 5:14pm

    lehrerfreund

    263 posts

    Thanks a lot, Iain, for this solution, which works perfectly. Also I learned from your example that I should use more often the ability to pass variables to templates!

    There is one little problem left: If I use the code as above there are shown the 3 most recent entries - and thus the entry to which it is related (see screenshot). The “order_by”-parameter seems not to work in this solution.
    Is it possible to start with the shown entries on some certain point (let’s say: the 5th newest entry of the category. or: a random entry, or: an entry below ID 400 ...)?

  • #5 / Apr 23, 2011 5:35pm

    iain

    317 posts

    Easiest would be to exclude the parent entry from the child, for example:

    {exp:channel:entries channel="foo" limit="3"}
    <h1>{title}</h1>
    {summary}
    {embed="inc/.related_articles" entry_id="{entry_id}" category_ids="{categories backspace='1'}{category_id}|{/categories}"}
    ....

    And then in your inc/.related_articles template:

    {if embed:category_ids}
    {exp:channel:entries channel="foo" limit="3" entry_id="not {embed:entry_id}" category="{embed:category_ids}"}
    ...
    {/if}

    All the regular channel:entries parameters in the embedded template will work fine as there’s nothing ‘different’ going on here. So order_by should be unaffected by anything we’re doing.

    I’d suggest also if you’re just showing a list of links, you use the disable parameter to disable custom fields in the embedded channel entries loop to make it less resource hungry, and don’t forget to use dynamic=“no” in the embedded channel entries tag too!

  • #6 / Apr 23, 2011 5:44pm

    lehrerfreund

    263 posts

    Hey - that was fast! And it works like a charm. Also indeed the orderby-parameter works, that it didn’t work before was my fault.

    So thank you very much - also for the hints on stabilizing performance, which one too often forgets!

    (my case closed)

  • #7 / Apr 24, 2011 4:15pm

    Greg Salt

    3988 posts

    Hi lehrerfreund,

    Glad that Iain was able to help you out. Please post back if you have further questions or problems.

    Cheers

    Greg

    @Iain, thanks very much.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases