6

I'm on a single product entry page. The URL does not specify the category. In fact, the entry has 3 category groups associated with it. I want to display the first category assigned from each of the category groups.

Here's the code I have so far:

{exp:channel:entries channel="products" limit="1" }
<div id="prodinfo">
    <h1>{title}</h1>

    {exp:channel:categories category_group="4" note="vehicle type"}
    {category_name}
    {/exp:channel:categories}

</div> <!-- END #prodinfo -->
{/exp:channel:entries}

This is displaying all the categories in the group. I've also tried using the {if active} conditional variable, but as it relies on the URI, it's not working for me.

Thanks for your help.

1 Answer 1

9

I think you're trying to use the wrong tag here.

That exp:channel:categories tag is for when you want to display a list of all the categories on your site - and possibly link to those "category listing" pages.

Here's how I display an individual entry's category:

{exp:channel:entries channel="spc-content" limit="1" rdf="off" show_future_entries="no" status="open|Featured" require_entry="yes"}
<h1>{title}</h1>
{categories}<a href="{path=obsessive/compulsions}">{category_name}</a> {/categories}
{/exp:channel:entries}

To change your code to display what you want, try this:

{exp:channel:entries channel="products" limit="1" }
<div id="prodinfo">
    <h1>{title}</h1>

    {categories}
    {category_name}
    {/categories}

</div> <!-- END #prodinfo -->
{/exp:channel:entries}
5
  • 1
    To expand you might want {categories limit="1"} to only show one tag - more: ellislab.com/expressionengine/user-guide/modules/channel/…
    – liamcrean
    Mar 21, 2013 at 15:51
  • Good point @llamcrean makes - if you want to display all the categories an entry is tagged with, my example works. If you only want to display one...use the limit parameter.
    – user800
    Mar 21, 2013 at 15:53
  • Yeah, I got that. Once @Leigh pointed me to the {categories} tag, I went and read the docs. Got everything happening the way I wanted. Thanks again. Mar 21, 2013 at 17:42
  • besides limit="1" any way to also restrict to a specific category group? category_group="1" doesn't seem to work for me when using the categories tag inside channel entries.
    – user3597
    Sep 19, 2014 at 5:06
  • You want show_group= instead: ellislab.com/expressionengine/user-guide/add-ons/channel/… Sep 19, 2014 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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