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.

Show Categories assigned to entries list

July 12, 2012 6:16am

Subscribe [1]
  • #1 / Jul 12, 2012 6:16am

    milk_

    9 posts

    Hi.
    I would like to show a dynamic menu of the categories applied to the results page.

    Let’s consider we have a total of 5 results.

    If I use the following code:

    {exp:channel:entries channel="products" style="linear"  status="open"}
    {exp:query sql="SELECT cat_name FROM exp_channel_data
        INNER JOIN exp_category_posts
        ON exp_category_posts.entry_id = exp_channel_data.entry_id 
                                    INNER JOIN exp_categories
        ON exp_categories.cat_id = exp_category_posts.cat_id 
        WHERE exp_category_posts.entry_id = {entry_id}
        "}
        <li class="effects"><a href="#data-filter=">{cat_name}</a></li>
    {/exp:query}
    {/exp:channel:entries}

    I get duplicated values (eg. Category One Category Two Category Three | Category One Category Two Category Three)
    Basically all the categories are listed once per result, but I would like to group them.

    I’ve been trying also with this code:

    {exp:channel:entries channel="products" style="linear"  status="open"}
    {exp:query sql="SELECT cat_name FROM exp_channel_data
        INNER JOIN exp_category_posts
        ON exp_category_posts.entry_id = exp_channel_data.entry_id 
                                    INNER JOIN exp_categories
        ON exp_categories.cat_id = exp_category_posts.cat_id 
        WHERE exp_category_posts.entry_id = {entry_id}
        GROUP BY cat_name"}
        <li class="effects"><a href="#data-filter=">{cat_name}</a></li>
    {/exp:query}
    {/exp:channel:entries}

    without success…

    Thanks in advance for your tips.


    Also, if you have a simpler way to achieve the result, without using SQL statements but basic exp:channel:categories tags, please give me an advice.

  • #2 / Jul 16, 2012 10:14am

    Dan Decker

    7338 posts

    Hi milk_,

    Thanks for posting your question on the Forums!

    Indeed, there is no need to use the Query module to achieve this. From your description, it sounds like the Category Archive tag would be exactly what you need:

    {exp:channel:category_archive channel="default_site" style="linear"}
        {categories}
            <h3>{category_name}</h3>
    <p>        {if category_description}<br />
                {category_description}<br />
            {/if}<br />
        {/categories}<br />
        {entry_titles}<br />
            <a href="http://{path=SITE_INDEX}">{title}</a></p>
    
    <p>    {/entry_titles}<br />
    {/exp:channel:category_archive}

    Which will give you output similar to:

    <h3>Category one</h3>
    <p>Category one is our first category.<br />
    <a href="http://www.mysite.com/1/">title one</a></p>
    
    <p><a href="http://www.mysite.com/2/">title two</a></p>
    
    <p><a href="http://www.mysite.com/3/">title three</a></p>
    
    <p><a href="http://www.mysite.com/4/">title four</a></p>
    
    <p><a href="http://www.mysite.com/5/">title five</a></p>
    
    <h3>Category two</h3>
    <p>This was our second category ever created.<br />
    <a href="http://www.mysite.com/6/">title one</a></p>
    
    <p><a href="http://www.mysite.com/7/">title two</a></p>
    
    <p><a href="http://www.mysite.com/8/">title three</a></p>
    
    <p><a href="http://www.mysite.com/9/">title four</a></p>
    
    <p><a href="http://www.mysite.com/10/">title five</a><br />
    

    Let me know what you think!

    Cheers,

     

  • #3 / Jul 17, 2012 6:40am

    milk_

    9 posts

    Hi Dan, thanks for your suggestion.
    I would like the list to be dynamic though. I don’t want all the categories to be listed, just the ones assigned to the results being viewed.
    Can you suggest how to actually do it?

    Edoardo

  • #4 / Jul 17, 2012 9:08am

    milk_

    9 posts

    If you read more accurately (sorry if I was not clear somehow) I want to show ONLY the categories being assigned to the results being listed.
    If a result shares common category with another result, the <li> in question should be listed only ONCE.

  • #5 / Jul 18, 2012 10:40am

    Kevin Smith

    4784 posts

    Hi milk_,

    Hmm… yes, what you’re asking is a bit confusing. I think it’d be easier to understand if you provide an example of what you’re trying to achieve. Can you show us the goal you’re going for? Then we can show you the best way to get there.

  • #6 / Jul 18, 2012 10:52am

    milk_

    9 posts

    I somehow managed to make it by using jquery to de-duplicate categories being shown… the final result is here:
    http://widerangetones.edoardobiasini.it/it/products/category/bearfoot

    See the tags that allow the filtering, above the results?
    Previously I would get
    Bearfoot Distortion Overdrive Compression Distortion Distortion Overdrive… ecc… as the above menu would be populated with ALL the categories, some of them were common between products…
    I managed to solve it (even if it’s not really elegant) by using Jquery:

    $('li[id^=cat_]').filter(function() {
        var ending = this.id.replace("cat_","");
        if( found.hasOwnProperty( ending ) ) {
            return this;
        } else {
            found[ ending ] = ending;
        }
    }).remove();

    If you have a more elegant Javascript-free solution, please send your suggestion.

  • #7 / Jul 18, 2012 5:21pm

    Kevin Smith

    4784 posts

    Oh ok, I see what you were going for. I think I’d recommend going back to the Category Archive tag like Dan suggested. You wouldn’t need all the extra stuff since all you want it to do is output the category name, but this tag will make sure you don’t have duplicate names.

    {exp:channel:category_archive channel="products" style="linear"}
        {categories}
            <li class="effects"><a href="#data-filter=">{category_name}</a></li>
        {/categories}
    {/exp:channel:category_archive}

    Does that make sense?

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

ExpressionEngine News!

#eecms, #events, #releases