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.

How to get category name or category id of currently viewed entry?

October 02, 2007 11:29am

Subscribe [1]
  • #1 / Oct 02, 2007 11:29am

    Brian Litzinger

    711 posts

    I have several navigation links that have sub pages, blog being one of them, and it’s easy to determine if I’m currently viewing a blog entry because “blog” exists in the URL. For example…

    <a href="/blog/" class="expandable {if segment_1 == ">Blog</a>

    But for my static pages (not using the Pages module) and blog entries I don’t know how to determine which category the currently viewed entry is in? I have this at the top of my templates…

    {if segment_2 != ""}
       {exp:weblog:entries weblog="{my_weblog}" orderby="date" dynamic="on" sort="desc" limit="1" url_title="{segment_2}"}
          {assign_variable:view="single"} 
          {assign_variable:page_title="{title}"}    
          {assign_variable:current_category="{category}"}
       {/exp:weblog:entries}
    {/if}

    You can see that if segment_2 isn’t blank, then I’m viewing a single entry, and I’m changing my layout a bit further down the page, and setting the page title, but the current_category isn’t being assigned, it’s just printing {category} on the page. Ideally I’d like my navigation code to look like this…

    <a href="/blog/" class="expandable {if current_category == ">Link to this category of entries</a>

    or

    <a href="/page/about/" class="expandable {if current_category == ">About</a>

    Does that make sense?

  • #2 / Oct 03, 2007 10:41pm

    Gonzalingui

    13 posts

    I’m not sure if this is the only way, but to get the category name (or id) you need first to open the {categories} tag, and get the data inside that tag.

    Maybe this example will help:

    {categories}
    {if '{category_name}' == 'food'}This is the food category{/if}
    {/categories}
  • #3 / Oct 04, 2007 10:55am

    Brian Litzinger

    711 posts

    I swear I tried that and it didn’t work, but I’ll give it another shot. Thanks.

  • #4 / Oct 04, 2007 11:26am

    Gonzalingui

    13 posts

    Well I’ll describe the context where I use that.

    I’m using all inside an {exp:weblog:entries} tag. And I don’t have “categories” disabled: {exp:weblog:entries disable="categories"}

    I’ll copy the code just how I’m using it in my template:

    {exp:weblog:entries weblog="catalogo_de_discos" orderby="date" sort="asc" limit="1" disable="member_data|pagination|trackbacks"}
    ...
    {categories}
    {if '{category_name}' == 'Latinoamericana'}Selección Latinoamericana{/if}
    {/categories}
    ...
    {/exp:weblog:entries}

    That works for me.

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

ExpressionEngine News!

#eecms, #events, #releases