admin管理员组

文章数量:1122846

Here is the code for the category breakcrumb:

/* Display breadcrumbs for single post */
$count_loop       = NULL;
$count_categories = NULL;
        if ( is_single() ) {
            $category = get_the_category();
            $num_cat = count($category);
            if ($num_cat <=1) {
                $output .= ' ' . html_entity_decode( esc_html( get_the_title() ) );
            } else {

                // Count Total categories
                foreach( get_the_category() as $category) {
                    $count_categories++;
                }
                
                // Output Categories
                foreach( get_the_category() as $category) {
                    $count_loop++;

                    if ( $count_loop < $count_categories ) {
                        $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->cat_name ) . '</a>' . $delimiter_inner; 
                    } else {
                        $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->cat_name ) . '</a>'; 
                    }
                }
                
                if (strlen(get_the_title()) >= $maxLength) {
                    $output .=  ' ' . $delimiter . esc_html( trim( substr( get_the_title(), 0, $maxLength ) ) ) . ' &hellip;';
                } else {
                    $output .=  ' ' . $delimiter . esc_html( get_the_title() );
                }
            }
        } 

For example, a post with 4 categories gives me: Base / Campaign / News / Tools / and the post title.

It works fine. However, it retrieves the result ordered by name. I would like to have it in a hierarchical order, considering that some of them are not children...

I expected having: News / Base > Tools > Campaign > post title.

Any idea how I can manage this with this piece of code I already have? Among others, I tried this:

$category = get_the_category();
$category = array_reverse( $category );

And this:

get_category_parents( $category, true, ' > ' );

Without any success. Thx for your help.

本文标签: Categories breadcrumb in a hierarchical order