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.

Displaying the name of a single subcategory

April 08, 2012 11:51am

Subscribe [4]
  • #1 / Apr 08, 2012 11:51am

    Tony Geer

    253 posts

    Hi there. I have my categories set up like this:

    Publishers
      - Pub1
      - Pub2
      - Pub3

    And so on, where Pub# are subcategories of the Publishers category. Each entry is assigned to a single Pub# and on an individual entry page I’m to get that publisher displayed.

    I’ve tried:

    {exp:query sql="SELECT cat_id as category_id, cat_name AS category_name FROM exp_categories WHERE parent_id = '2'"}
         {category_name}
        {/exp:query}

    from the Wiki but that actually displays all subcats. The Query module isn’t my strongest area, so if anyone can help I’d appreciate it!

  • #2 / Apr 08, 2012 12:05pm

    This is @objectivehtml on Twitter.

    I ran the query in SQLPro on my end, the query itself is fine. It returns the expected results.

    Not really sure where your going wrong, but my suggestion would to be to use GWCode Categories. Avoid the query module, IMO it’s not good to use in a template. GWCode Categories a great add-on for interacting with categories (probably the best category related add-on).

    http://gwcode.com/add-ons/gwcode-categories

  • #3 / Apr 08, 2012 1:22pm

    the3mus1can

    426 posts

    Try something like this (untested):

    {exp:query sql="SELECT c.cat_id as category_id, c.cat_name AS category_name FROM exp_categories as c JOIN exp_category_posts as cp ON cp.cat_id = c.cat_id WHERE c.parent_id = '2' AND cp.entry_id = {entry_id}"}
         {category_name}
    {/exp:query}
  • #4 / Apr 08, 2012 1:30pm

    Tony Geer

    253 posts

    Musician: That worked perfectly, thanks! One of these days I’ll have to take the time to learn some SQL…

    Objective: I tried GWcode Categories and I couldn’t quite get the results I needed (as I actually have two sets of parent categories in there, for publishers and authors) so I posted on the GWcode Cats forum on Devot:ee.

    I’ll use the query for now and if I get an answer using the other plugin I’ll compare the two. Thanks to you both!

  • #5 / Apr 11, 2012 2:16am

    Laisvunas

    879 posts

    Hi Tony,

    To output parent categories and subcategories and their entries you can use Child Categories plugin.

  • #6 / Apr 11, 2012 11:17am

    Tony Geer

    253 posts

    Thanks Laisvunas. I actually got pretty great responses from Leon over on the GWcode forum on how to get this accomplished. So for future reference if anyone needs similar functionality they can check here: http://devot-ee.com/add-ons/support/gwcode-categories/viewthread/5245

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

ExpressionEngine News!

#eecms, #events, #releases