Hierarchical List of Terms like breadcrumbs

  • Hi there
    I use Page Hero block built with GenerateBlocks Pro (v. 2.3)
    My categories are hierarchical (core WP posts).
    I’d like to alter my post meta category links {{term_list link:term|tax:category}}
    from:
    Child, Grandparent, Parent (if they are all checked on)
    or
    Child (if only the most specific category is selected)

    to:
    Grandparent > Parent > Child

    Please help

  • Hi there,

    GB does not have this option to order the term list, unfortunately!

  • No filter can be applied?

  • Hi there,

    That’s currently not possible — there isn’t a filter available that can be used for that use case, unfortunately.

  • OK, I understand.

    I’d like to share a solution that we have figured out with ChatGPT 😉
    It works in regular GeneratePress theme / GP Premium (archives, search results, single posts), not in GenerateBlocks.

    /**
     * Filter post categories output to display hierarchy (Parent > Child),
     * removing standalone parents if they already appear in a child chain.
     * Wrap each chain in <span class="chain">…</span>.
     */
    add_filter( 'the_category', function( $thelist, $separator = ', ', $parents = '' ) {
        global $post;
    
        // Do nothing in backend
        if ( is_admin() ) {
            return $thelist;
        }
    
        if ( ! $post ) {
            return $thelist;
        }
    
        $categories = get_the_category( $post->ID );
    
        if ( empty( $categories ) || is_wp_error( $categories ) ) {
            return $thelist;
        }
    
        // Collect all parent IDs used in any chain
        $parents_in_chains = [];
    
        // Build all category chains
        $chains = [];
        foreach ( $categories as $cat ) {
            $chain   = [];
            $current = $cat;
    
            while ( $current ) {
                $chain[] = $current;
                $current = $current->parent ? get_category( $current->parent ) : null;
            }
    
            $chain = array_reverse( $chain );
            $chains[] = $chain;
    
            // Save parent IDs (exclude the leaf itself)
            foreach ( array_slice( $chain, 0, -1 ) as $parent_term ) {
                $parents_in_chains[] = $parent_term->term_id;
            }
        }
    
        $output = [];
    
        foreach ( $chains as $chain ) {
            $leaf = end( $chain );
    
            // Skip standalone parents if they already appear in another chain
            if ( in_array( $leaf->term_id, $parents_in_chains, true ) ) {
                continue;
            }
    
            // Convert to linked names
            $names = array_map( function( $term ) {
                return sprintf(
                    '<a href="%s" rel="category tag">%s</a>',
                    esc_url( get_category_link( $term->term_id ) ),
                    esc_html( $term->name )
                );
            }, $chain );
    
            // Wrap in span
            $output[] = '<span class="chain">' . implode( ' > ', $names ) . '</span>';
        }
    
        return implode( $separator, $output );
    }, 10, 3 );
  • Thanks for sharing 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.