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.

Categories and URL segments question

July 11, 2007 12:16am

Subscribe [2]
  • #1 / Jul 11, 2007 12:16am

    Tyssen

    756 posts

    I’ve got two categories - news & events - and I’m using the same template group to display both. URLS for them look like:

    /news/category/latest-news/
    /news/category/events/

    On the index page for displaying these I’ve got:

    {exp:weblog:entries weblog="news"}
    {embed="includes/header" {if segment_3=="latest-news"} the_title="Latest news" {if:else} the_title="Latest events" {/if}}
    {/exp:weblog:entries}
                <div id="text">
                    <h1>Latest {if segment_3=="latest-news"} News {if:else} Events {/if}</h1>
                    {exp:weblog:entries weblog="news" orderby="date" sort="desc" limit="10" disable="member_data|trackbacks" rdf="off"}    
                    <h2><a href="http://{title_permalink=news/item}">{title}</a></h2>
    <p>                {entry_date format='%F %j%S, %Y'}<br />
                    {summary}<br />
                    <a href="http://{title_permalink=news/item}">Read more about {title}</a></p>
    
    <p>                {paginate}<br />
                    {if previous_page}<br />
                    <a href="http://{auto_path}">Previous entries</a><br />
                    {/if}<br />
                    {if next_page}<br />
                    <a href="http://{auto_path}">Next entries</a><br />
                    {/if}<br />
                    {/paginate}                <br />
                    {/exp:weblog:entries}                <br />
                </div><br />
                <div id="sidebar"><br />
                    <div class="inner"><br />
                        {if segment_3=="latest-news"}<br />
                        </p><h2>Other news</h2><p>                    <br />
                        <ul><br />
                        {exp:weblog:category_archive weblog="news" show="2" style="linear"}<br />
                        {entry_titles}<br />
                          <li><a href="http://{path=news/item}">{title}</a></li><br />
                          {/entry_titles}<br />
                        {/exp:weblog:category_archive}<br />
                        </ul><br />
                        {/if}<br />
                        {if segment_3=="events"}<br />
                        </p><h2>Other Events</h2><p>                    <br />
                        <ul><br />
                        {exp:weblog:category_archive weblog="news" show="4" style="linear"}<br />
                        {entry_titles}<br />
                          <li><a href="http://{path=news/item}">{title}</a></li><br />
                          {/entry_titles}<br />
                        {/exp:weblog:category_archive}<br />
                        </ul><br />
                        {/if}<br />
                    </div><br />
                    <div id="side-btm"></div><br />
              </div><br />
                {if segment_3=="latest-news"}<a href="http://{path=/news/c/events}">View events</a>{/if}<br />
                {if segment_3=="events"}<a href="http://{path=/news/c/latest-news}">View news</a>{/if}<br />
    {embed="includes/footer"}

    You can see that I’m using segment 3 to determine which category I’m in and display different info for each. But the template structure doesn’t work the same way. The template I’m using to display individual content is news/item - no 3rd segment. (At the moment the item template has the same sidebar code as the indext template and that’s the bit I’m concerned about at the moment.)

    So how do I go about extracting the info that I need based on the 3rd segment on the item template if I don’t actually have a 3rd segment? Or is there a better way to put this together?

  • #2 / Jul 11, 2007 4:36am

    silenz

    1651 posts

    What info is it that you need on the item template? Which category the entry belongs to?
    Since a single entry can belong to multiple categories you can only figure it out with categories-tagpair inside exp:weblog:entries.

    {exp:weblog:entries}
     {categories}
      {category_name}
     {/catgeories}
    {/exp:weblog:entries}
  • #3 / Jul 11, 2007 4:41am

    Tyssen

    756 posts

    I just want to display links to other entries in the same category AND a single link to the category page for the other category (ie not the current one).

  • #4 / Jul 11, 2007 5:29am

    silenz

    1651 posts

    I just want to display links to other entries in the same category

    This is easy. Just put

    {categories}<a href="http://{path=news/index}">{category_name}</a> {/categories}

    inside a weblog:entries-tag.

    AND a single link to the category page for the other category (ie not the current one)

    .

    This is a little tricky. If you put this into weblog:entries-tag

    {exp:weblog:categories weblog="news" style="linear"  show="not {categories}{category_id}|{/categories}"}
    <a href="http://{path=news/index">{category_name}</a>
    {/exp:weblog:categories}

    it will show links to all categories the current entry does not belong to.

    In your case, with only two categories that means it will only link to the other category.

  • #5 / Jul 11, 2007 7:00am

    Tyssen

    756 posts

    This is easy. Just put

    {categories}<a href="http://{path=news/index}">{category_name}</a> {/categories}

    inside a weblog:entries-tag.

    That gives me links to the category; I want links to articles attached to the category of the article you’re currently viewing, not the category itself.

  • #6 / Jul 11, 2007 7:40am

    Daniel Walton

    553 posts

    try using another weblog entries tag pair on this template, with Related Categories Mode specified.

  • #7 / Jul 11, 2007 8:01am

    Tyssen

    756 posts

    try using another weblog entries tag pair on this template, with Related Categories Mode specified.

    That sounds like it should do what I want but I must be doing something wrong because nothing shows up using what’s below:

    {exp:weblog:entries weblog="news"}
    {embed="includes/header" the_title="{title}"}
    {/exp:weblog:entries}
    
                <div id="text">
                    {exp:weblog:entries weblog="news" orderby="date" sort="desc" limit="50" disable="member_data|trackbacks" rdf="off"}    
                    <h1>{title}</h1>
                    {entry_date format='%F %j%S, %Y'}
                    {body}
                    {/exp:weblog:entries}
                    <a href="http://{title_permalink=news/c/news}">Back to news</a>                
                </div>
                <div id="sidebar">
                    <div class="inner">
                        <ul>
                        {exp:weblog:entries related_categories_mode="on" custom_fields="on" member_data="on"}
                            <li><a href="http://{path=news/item}">{title}</a></li>
                        {/exp:weblog:entries}
                        </ul>
                    </div>
                    <div id="side-btm"></div>
              </div>
    {embed="includes/footer"}

    It doesn’t work with {exp:weblog:entries weblog="news" related_categories_mode="on"} either.

  • #8 / Jul 11, 2007 8:05am

    Daniel Walton

    553 posts

    Is this a single entry page? (entry_id or url_title in the url?)

  • #9 / Jul 11, 2007 8:12am

    Tyssen

    756 posts

    Sorry, the reason nothing was showing up was because there weren’t any other entries in the category. Duh! :slaps head:

    But my next question would be, if I wanted to add a heading to this list, e.g. ‘Other news’ or ‘Other events’, how would I do that?

  • #10 / Jul 11, 2007 8:29am

    Daniel Walton

    553 posts

    Because of the way your html is structured, you could just hard code that above the <ul>.

    Alternatively, you could restructure it slightly, so to not show anything if empty:

    {exp:weblog:entries related_categories_mode="on" custom_fields="on" member_data="on"}
    {if count = 1}
    <div id="sidebar">
        <div class="inner">
        <h2>Other things…</h2>
    <p>        <ul><br />
    {/if}<br />
                <li><a href="http://{path=news/item}">{title}</a></li><br />
    {if count == total_results}<br />
            </ul><br />
        </div><br />
        <div id="side-btm"></div><br />
    </div><br />
    {/if}<br />
    {/exp:weblog:entries}

  • #11 / Jul 11, 2007 9:34am

    Tyssen

    756 posts

    Because of the way your html is structured, you could just hard code that above the <ul>.

    But I want it to say ‘Other news’ when you’re on the single post page for an item in the ‘news’ category and ‘Other events’ when you’re on the single post page for an item in the ‘events’ category so it needs to be conditional some how.

  • #12 / Jul 11, 2007 9:50am

    Daniel Walton

    553 posts

    Ah right, well here you can use:

    {exp:weblog:entries related_categories_mode="on" custom_fields="on" member_data="on"}
    {if count = 1}
    <div id="sidebar">
        <div class="inner">
        <h2>Other {categories}{category_name}{/categories}...</h2>
    <p>        <ul><br />
    {/if}<br />
                <li><a href="http://{path=news/item}">{title}</a></li><br />
    {if count == total_results}<br />
            </ul><br />
        </div><br />
        <div id="side-btm"></div><br />
    </div><br />
    {/if}<br />
    {/exp:weblog:entries}

    since the entry is only ever going to be in either news, or events.

  • #13 / Jul 11, 2007 10:02am

    silenz

    1651 posts

    {categories}{category_name}{/categories} seems not to work in Related Categories Mode ....

  • #14 / Jul 11, 2007 10:06am

    Tyssen

    756 posts

    {categories}{category_name}{/categories} seems not to work in Related Categories Mode ....

    No it doesn’t so what I’ve done is this:

    <div id="sidebar">
                    <div class="inner">
                        {exp:weblog:entries weblog="news"}
                        <h2>Other {categories}{category_name}{/categories}</h2>
    <p>                    {/exp:weblog:entries}<br />
                        <ul><br />
                        {exp:weblog:entries weblog="news" related_categories_mode="on"}<br />
                            <li><a href="http://{title_permalink=news/item}">{title}</a></li><br />
                        {/exp:weblog:entries}<br />
                        </ul><br />
                    </div><br />
                    <div id="side-btm"></div><br />
              </div><br />
    {embed="includes/footer"}

    and that’s working although it seems a little awkward having to open and close the weblog entries just to get a category title.

    I’d also like to extract only part of the title because the category title is ‘Latest news’ (to avoid a conflict with the weblog title of ‘news’) but I want the heading to say ‘Other news’ and not ‘Other latest news’.

  • #15 / Jul 11, 2007 10:12am

    Daniel Walton

    553 posts

    Quick answer to that would be to rename the category to just ‘News’, since an entry in that category, that is 6 months old is hardly ‘latest’ ;]

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

ExpressionEngine News!

#eecms, #events, #releases