0

Either this is harder than it needs to be or I am just not understanding WordPress/PHP very well :( All I want to do is show the child/sub categories of a specific parent category...but only if the post is in those subcategories. Specific example:

I am building a wine reviews website and these are the categories:

  • Brand
    • Subcategory 1
    • Subcategory 2
    • etc.
  • Region
    • Subcategory 1
    • Subcategory 2
    • etc.
  • Grape
    • Subcategory 1
    • Subcategory 2
    • etc.

The parent categories will never change, and every post will have at least 1 subcategory selected under each parent, so in the LOOP I can just list the parents by name. But I am needing to dynamically output the subcategories, something like this:

Brand: <?php list_post_subcategories_of('brand'); ?>
Region: <?php list_post_subcategories_of('region'); ?>
Grape: <?php list_post_subcategories_of('grape'); ?>

Is there any easy way like this? It seems like this should be a basic function in Wordpress? I've looked at the functions 'get_categories' and 'in_category' but they don't seem to be able to do this.

1 Answer 1

0

I would personally create three custom taxonomies rather than do this with categories/subcategories, then this becomes a non-issue.

To do this with categories/subcategories, start with the names/IDs of your parent categories, and loop over each parent, then within that, loop over the terms associated with the post and check the term's parent ID against the top level category's ID:

// names / IDs of parents
$parents = array(
    'Brand' => 42,
    'Region' => 22,
    'Grape' => 18
);
// get post categories
$categories = get_the_terms( $post->ID, 'category' );
// output top level cats and their children
foreach( $parents as $parent_name => $parent_id ):
    // output parent name and link
    echo '<a href="' . get_term_link( $parent_id, 'category' ) . '">' . $parent_name . '</a>: ';
    // initialize array to hold child links
    $links = array();
    foreach( $categories as $category ):
        if( $parent_id == $category->parent ):
            // put link in array
            $links[] = '<a href="' . get_term_link( $category ) . '">' . $category->name . '</a>';
        endif;
    endforeach;
    // join and output links with separator
    echo join( ', ', $links );
endforeach;

EDIT- dynamically fetch top level terms:

// get top level terms
$parents = get_terms( 'category', array( 'parent' => 0 ) );
// get post categories
$categories = get_the_terms( $post->ID, 'category' );
// output top level cats and their children
foreach( $parents as $parent ):
    // output parent name and link
    echo '<a href="' . get_term_link( $parent ) . '">' . $parent->name . '</a>: ';
    // initialize array to hold child links
    $links = array();
    foreach( $categories as $category ):
        if( $parent->term_id == $category->parent ):
            // put link in array
            $links[] = '<a href="' . get_term_link( $category ) . '">' . $category->name . '</a>';
        endif;
    endforeach;
    // join and output links with separator
    echo join( ', ', $links );
endforeach;
10
  • wow, awesome! Thank you. I've been looking for something like that for 2 days. Thank you for being here to help. It's too bad someone downvoted my question when I created an account to ask this b/c I could not find an answer anywhere and do not have the uber PHP skills like yourself. Thanks again. p.s - I just had to add commas after the first 2 array items (42 and 22) to get it to work.
    – codeview
    Oct 20, 2013 at 17:38
  • also, why would it be a non-issue with custom taxonomies?
    – codeview
    Oct 20, 2013 at 17:40
  • ah sorry for the typo, I hadn't tested it. if you use custom taxonomies, your subcategories would then be top level terms in 3 distinct taxonomies, so you would just have to output all the associated terms within each without having to sort them by a parent term.
    – Milo
    Oct 20, 2013 at 17:53
  • Milo - thanks again. Would you be able to help me with making the parent and subcategories links to their respective pages, as well as show a separator when there is more than 1 category showing (comma or bullet)?
    – codeview
    Oct 21, 2013 at 2:25
  • sure, see edit above. untested, but I think it should work!
    – Milo
    Oct 21, 2013 at 2:47

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.