Support » Plugins » Hacks » Get the Category List in Hierarchical order

  • Resolved digitalrenewal

    (@digitalrenewal)


    I’m having a heck of a time figuring this out. I’ve been all over the codex and every forum…

    I want to get a list of categories and their children for each post.
    Anyone know how to do this?

    it seems like there should be a simple solution but the simple ones aren’t quite perfect…

    Here’s a screenshot of

    echo wp_list_categories();
    and
    echo get_the_category_list();

    SO CLOSE!

    I need get_the_category_list(); to be nested in hierarchical order

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m afraid there’s no handy function to make a hierarchical list of a post’s categories. I don’t think the WP devs imagined there being much need since typically people don’t assign so many terms to a post for it to be necessary. Fringe needs are typically left to plugin devs. I’m not aware of any such plugin either, but I’m not very knowledgeable about available plugins, so it may be worth some searching.

    Unless you can find a plugin, I think you’re going to need to develop a custom function to do this. The Walker_Category class can be used to generate hierarchical term lists. I don’t have any experience using walker classes, so other than pointing you in this direction, I’m not able to be much help. If you cannot get this class to do what you want, you can extend it in order to override any methods that don’t do what you need.

    Thread Starter digitalrenewal

    (@digitalrenewal)

    hmmmm. ok. Maybe I should rethink the structure then. I usually don’t use so many categories, but I have a ton of content I want to cross reference.

    I’m using the divi theme. I can put my database in a ‘project’ portfolio. I’d hate to use custom post types then eventually change the theme.

    I just thought of something I’ll try before I go to that extreme. I do like wp_list_categories layout. Maybe I can exclude the ones i dont want. I’ll give it a shot.

    Thread Starter digitalrenewal

    (@digitalrenewal)

    OK that actually worked!!!

    this is my code, i got it from here.
    https://developer.wordpress.org/reference/functions/wp_list_categories/

    	<?php wp_list_categories( array(
    		'title_li' => '<h2>' . __( 'Uses', '' ) . '</h2>',
                    'exclude'    => array( 1 )
             ) ); ?> 

    so everything in category 1, plus its child categories will not be listed. perfect!
    and of course i’m using tags too to organize content further

    <?php echo get_the_tag_list( '', '<br />', '' ); ?>

    Thread Starter digitalrenewal

    (@digitalrenewal)

    ONE last thing. I needed a conditional statement to show category and tag lists ONLY on certain posts.
    I needed a way to include subcategories.

    I found the answer here:
    http://wordpress.stackexchange.com/questions/142406/if-parent-category-is-conditional

    heres the code:

    <?php
    	if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
        function post_is_in_descendant_category( $cats, $_post = null ) {
            foreach ( (array) $cats as $cat ) {
                // get_term_children() accepts integer ID only
                $descendants = get_term_children( (int) $cat, 'category' );
                if ( $descendants && in_category( $descendants, $_post ) )
                    return true;
            }
            return false;
        }
    }
    if ( in_category( 'blog' ) || post_is_in_descendant_category( 1 ) ) {
      // do nothing
    } else {  ?>
    		<?php wp_list_categories( array(
    		'title_li' => '<h2>' . __( 'Uses', '' ) . '</h2>',
                    'exclude'    => array( 1 )
        ) );
    } ?>
    Moderator bcworkz

    (@bcworkz)

    Nice! I’m glad you found a solution. Thanks for reporting back.

    I’ll go ahead an mark this topic as resolved 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get the Category List in Hierarchical order’ is closed to new replies.