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 do I output the category name that the current entry belongs to?

June 20, 2011 2:42pm

Subscribe [4]
  • #1 / Jun 20, 2011 2:42pm

    dgink

    60 posts

    Have gone through the documentation but not found a solution.

    {exp:channel:entries channel="movies"}
            
            <div class="category">
                {categories}
                            <a href="http://{path=/movies/wiki}">{category_name}</a>
                {/categories}                                
                  </div> <!-- end category -->
                  
            {/exp:channel:entries}

    This is spitting out all of the categories within the movies channel. I want only the one(s) for each movie entry. This is in a template that outputs multiple movie entries, each of which has one or multiple categories attached to it.
    Could anyone please help? Am I missing something?

  • #2 / Jun 20, 2011 4:36pm

    I’m sure there’s a less hacky way of doing it, but, this is what we do…

    {exp:query sql="SELECT exp_categories.cat_name AS cat_name, exp_categories.cat_url_title AS cat_url FROM exp_categories, exp_category_posts WHERE exp_categories.cat_id = exp_category_posts.cat_id AND exp_category_posts.entry_id = '{entry_id}'" backspace="2"}<a href="http://{path=%27/template-group/category/{cat_url">{cat_name}</a>, {/exp:query}

    That’ll output a comma separated list of the categories an entry belongs to.  You’ll have to enable the query module if it isn’t already.

  • #3 / Jun 20, 2011 7:38pm

    Mark Bowen

    12637 posts

    Take a look at the absolutely fantastic GWcode Categories add-on. There literally isn’t another add-on like this one.

    It will do exactly what you need here and an absolute ton more.

    Best wishes,

    Mark

  • #4 / Jun 20, 2011 8:00pm

    Boyink!

    5011 posts

    You shouldn’t need an addon for this.  Your code sample should be correct - the purpose of the {categories} looping variable pair is to do exactly what you are looking for - output the entries a single category is assigned to.

    As a test try:

    {exp:channel:entries channel="movies"}
    {title} <br / >     
           {categories}<a href="http://{path=/movies/wiki}">{category_name} | </a>{/categories}                                     
    <hr >
                  
    {/exp:channel:entries}

    The {exp:channel:categories} tag is for outputting all categories in a channel.

  • #5 / Jun 20, 2011 8:30pm

    Mark Bowen

    12637 posts

    Oops sorry Mike is quite correct. I had completely mis-read what you were after.

    That said the add-on I linked to is absolutely fantastic and will definitely allow you to do more than you can ever do with the built-in categories tags.

    Best wishes,

    Mark

  • #6 / Jun 20, 2011 11:59pm

    dgink

    60 posts

    Your code sample should be correct - the purpose of the {categories} looping variable pair is to do exactly what you are looking for - output the entries a single category is assigned to.

    Yes it was Mike. Awesome - I got exactly what I wanted. As a newbie I keep separating channel entries related information from category related info. Would it be right to assume that both are served up in the same bucket? As in are both available on the same page with the use of ee’s channel entries tag?

    @Bowen: I already have the GWcode plugin installed, professor. Would like to know how to do what I am doing right now using the plugin too. Went through the examples but most of them seemed like they were about spitting category trees. How would I output entry-category related information?

    @NickBenson - Thanks for the advanced hack - I got to start learning the query module too. This snippet should get me started.

  • #7 / Jun 21, 2011 12:09am

    Boyink!

    5011 posts

    Yes it was Mike. Awesome - I got exactly what I wanted. As a newbie I keep separating channel entries related information from category related info. Would it be right to assume that both are served up in the same bucket? As in are both available on the same page with the use of ee’s channel entries tag?

    It’s a bit of a paradigm jump, but you need to understand that anything in EE’s content database is available on any page.  Pages don’t store content.  Pages present content - through the templates that you code.  Templates are the connection point between EE content and final “pages” - but they are entirely defined by you and the EE tags you use.

    Conceptually entries do and don’t live in the same bucket as categories.  Entries get categorized, but a category group can be applied to more than one channel, so the same categories could be used in multiple buckets.

    Categories are simply a filtering mechanism - a way to select a sub-set of entries in a channel, with some meta to describe the filter.

  • #8 / Jun 21, 2011 12:16am

    dgink

    60 posts

    Pages don’t store content.  Pages present content - through the templates that you code.  Templates are the connection point between EE content and final “pages” - but they are entirely defined by you and the EE tags you use.

    Now that’s exactly the kind of insight I was looking for. You should teach man - oops!! you already do 😊

  • #9 / Jun 21, 2011 6:42am

    John Henry Donovan

    12339 posts

    Thanks Mike. Excellent insight as always. Feel free dgink, to start anew thread if you have any more questions

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

ExpressionEngine News!

#eecms, #events, #releases