1

The search results feature allows you to use {auto_path} to take you to the correct template_group/template, however, I need a similar solution for regular garden variety channel entries.

I have a news feed section on the home page that displays the 3-5 most recent entries from five different channels. The code is like this:

<h4>Recent News Posts</h4>
<ul class="inline-list">
  {exp:channel:entries channel="community|government|education|business|history" limit="3"}
    <li><a href="/news/{url_title}">{title}</a></li>
   {/exp:channel:entries}
</ul>

Currently I have a generic "news" placeholder in the path, but what I need is to direct each entry to its appropriate section. Outside the search results {auto_path} I have not been able to find anything. I am open to suggestions.

1

I may be reading this wrong and so if I am then I apologise but could this not work for you?

  <h4>Recent News Posts</h4>
<ul class="inline-list">
  {exp:channel:entries channel="community|government|education|business|history" limit="3"}
    <li><a href="/{channel_short_name}/{url_title}">{title}</a></li>
   {/exp:channel:entries}
</ul>

You then won't need to use any conditionals which add extra processing time to your template and if you ever add more channels then you won't need to add any more conditionals into the code either.

| improve this answer | |
  • Hi Mark, what a blast from the past - the old days on the EE forums. Welcome to the SE forum and thanks for the more efficient solution. Alex's solution worked, but this one is more efficient and extensible. Thanks. – forrest Mar 10 '15 at 18:35
1

One way is to use a conditional based on channel_short_name like this:

{exp:channel:entries channel="community|government|education|business|history" limit="3"}
    <li>
        {if channel_short_name == "news"}
            <a href="/news/{url_title}">{title}</a>
        {if:elseif channel_short_name == "community"}
            <a href="/community/{url_title}">{title}</a>
        {if:elseif channel_short_name == "government"}
            <a href="/government/{url_title}">{title}</a>
        {/if}
    </li>
{/exp:channel:entries}

For the sake of the example I just did three of your channels, but you'd want to add a clause in the conditional for each one.

| improve this answer | |
  • Alex, thank you for the solution. Exactly what I needed. – forrest Mar 6 '15 at 18:41
  • 1
    I would just add that if the template code within a matching conditional is anything more than very simple (eg. if it involves additional database queries, etc) then you might be better replacing your {if:elseif} sequence with Switchee (devot-ee.com/add-ons/switchee). The reason is that the native if/ifelse routines process all the template code for each conditional, whether it matches or not, and only at the end does EE discard the unmatched stuff. That's wasteful. Switchee only processes the code inside the matching condition, making your page more efficient. – Rick Lecoat Mar 8 '15 at 0:03
  • @Rick I believe this was ameliorated with the new conditional parser in v2.9. – Alex Kendrick Mar 9 '15 at 16:22
  • @Alex: I think it's been partially ameliorated by 2.9's conditional changes (no more 'simple' and 'advanced' conditionals). But Mark Croxton illustrates how, in many circumstances, a plug-in like Switchee or ifelse is still required to get a template to parse the way you want it to: gist.github.com/croxton/9d012297096892ca5c10 – Rick Lecoat Mar 9 '15 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.