3

Use custom category listing template 'news/category':

/site/news/category

Use custom template to present “Legal Politics” 'news/category-legal-politics' entries:

/site/news/category/politics/legal

I assume that I must create a custom template called "news/category" which then dynamically picks between:

  • category-listing
  • articles-in-category
  • special-news-category

And I am also guessing that custom URL fragments will need to come into play to achieve this. But what I do not understand how to convert the URL into the category ID:

<!-- Template Group: news   Template: category -->
{exp:channel:entries channel="news" category="??{get-from-url}??"}
    <h1><a href="{path='news/index'}">{title}</a></h1>
    <div class="summary">{summary}</div>
{/exp:channel:entries}

Interestingly I find that the following URL works (almost) for regular category listing

/site/news/category/category/politics/legal

when using the following template:

{exp:channel:entries channel="news"}
    <h1><a href="{path='news/index'}">{title}</a></h1>
    <div class="summary">{summary}</div>
    <ul class="categories">
        {categories}
            <li>{category_name}</li>
        {/categories}
    </ul>
{/exp:channel:entries}

If I could somehow skip the second category slug in the URL then this would be half way there!

In WordPress this would be achieved by providing a custom template "category.php" and special categories "category-legal-politics.php" or "category-42.php". What would the equivalent be for EE?

4

Why not use a single template for your news items with a conditional to check for the "/politics" or "/legal" URI segments? Your template would look something like this:

{!-- Category Index --}
{if !segment_3}
    Show overview of all content instead of simple listing...

{!-- Specific Category --}
{if:elseif segment_3 == "politics"}
    Specialised template for politics...

    {exp:channel:entries channel="news" category="3"} {!-- this assumes the "politics" category id is 3 --}
        <h1><a href="{path='news/index'}">{title}</a></h1>
        <div class="summary">{summary}</div>
    {/exp:channel:entries}

{!-- Default Category Listing --}
{if:else}
    Generic template for anything else...

    {exp:channel:entries channel="news" category="{segment_3_category_id}"}
        <h1><a href="{path='news/index'}">{title}</a></h1>
        <div class="summary">{summary}</div>
    {/exp:channel:entries}
{/if}
| improve this answer | |
  • How does the 3rd line in your example work (see category parameter at end) many thanks – Lea Hayes Dec 5 '12 at 3:46
  • Ah its fine, just use {segment_3}, thanks! – Lea Hayes Dec 5 '12 at 3:59
  • I have updated your source example to one that resolves my particular issue. Thanks very much for showing me the ropes here. I even learnt that EE has its own type of comments, awesome! – Lea Hayes Dec 5 '12 at 4:06
  • 1
    The one problem with your last two conditionals won't work without Low Seg2Cat. This free add-on lets you figure out the category_id form the segment URI which is what the category parameter in is looking for. With the add-on installed your opening exp:channel:entries tag should read {exp:channel:entries channel="news" category="[politics category id here]"} and {exp:channel:entries channel="news" category="{segment_3_category_id}"} respectively – Stuart McCoy Dec 5 '12 at 4:39
  • 1
    the category parameter is looking for the category id so in the case of politics it might be something like category="3". This assumes that your politics category id is 3. Low Seg2Cat, in this case, lets you pass a segment URI variable to the category parameter and converts it to the category id. This makes working with URI segments and categories is flexible. You should read the docs on the Low Seg2Cat to learn what else it can do. – Stuart McCoy Dec 5 '12 at 5:00

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.