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.

Reverse {categories} or {exp:channel:categories} output

April 07, 2011 6:49am

Subscribe [2]
  • #1 / Apr 07, 2011 6:49am

    TheBlackHole

    72 posts

    Hi all. Is it possible to reverse the output of either the {categories} or the {exp:channel:categories}.

    I’m building my meta page <title> on a single entry page and I need to output the parent and child categories in reverse, so I end up with this format…

    title | child category | parent category | template group | site name

    But since those tags output parent then child and seem to have no sort option I’m stumped!

    Thanks

  • #2 / Apr 07, 2011 2:34pm

    Leon Dijk

    131 posts

    Hi Mike,

    You can always use a query to get what you want.
    Assuming your child categories have only 1 parent category (the parent category is a root category), you can use this to get the parent category for an entry:

    {exp:query sql="SELECT cat_name AS parent_name, cat_url_title AS parent_cat_url_title FROM exp_categories c, exp_category_posts cp WHERE c.cat_id=cp.cat_id AND c.parent_id=0 AND cp.entry_id={entry_id}"}<a href="http://{parent_cat_url}">{parent_cat}</a>{/exp:query}

    And, to get your child category, assuming your entry has only been added to 1 child category, you can use this:

    {categories}
    {if parent_id != 0}
    <a href="http://{category_url_title}">{category_name}</a>
    {/if}
    {/categories}

    Hope that helps!

  • #3 / Apr 08, 2011 7:01am

    TheBlackHole

    72 posts

    Hi Leon,

    The query didn’t output anything, the parent is the root category too. The child category worked great though.

    Cheers
    Mike

  • #4 / Apr 08, 2011 1:21pm

    Leon Dijk

    131 posts

    Hi Mike,

    Woops sorry, I messed up the names there.

    Try this instead:

    {exp:query sql="SELECT cat_name AS parent_name, cat_url_title AS parent_cat_url_title FROM exp_categories c, exp_category_posts cp WHERE c.cat_id=cp.cat_id AND c.parent_id=0 AND cp.entry_id={entry_id}"}<a href="http://{parent_cat_url_title}">{parent_name}</a>{/exp:query}

    This will only work if you assign an entry to both the child category and the parent category, but based on our PM’s, I already know you are 😊

    Cheers,
    Leon

  • #5 / Apr 11, 2011 4:58am

    TheBlackHole

    72 posts

    Ah brilliant, thanks Leon.

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

ExpressionEngine News!

#eecms, #events, #releases