Skip Navigation

[Resolved] How to display parent category before child category? (not alphabetical order)

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display my list of terms in hierarchical order instead of alphabetical order.

Solution: Add the following code to functions.php:

unction list_hierarchical_terms($atts) {
    global $post;
    $taxonomy = $atts['taxonomy']; // change this to your taxonomy
    $separator = '';
    $out = '';
    $term_ids = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    usort( $term_ids, 'cmp' );
    foreach($term_ids as $term_id ) {
      $term = get_term( $term_id, $taxonomy, OBJECT );
      $out .= $separator . $term->name;
      $separator = $atts['separator'];
    }
    return $out;
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');

Then use your new custom shortcode like so:

[display_post_tax_terms_hierarchical taxonomy="category" separator=" | "]
This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by Christian Cox 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#573213

I am trying to:
To always display the parent category first

here is my code:
<h3><span class="cat-grid [wpv-post-taxonomy type='category' format='slug' separator=' ']">[wpv-post-taxonomy type="category" separator=" - "]</span><br />[wpv-post-link]</h3>

Link to a page where the issue can be seen:
hidden link

I expected to see:
My parent category is "Planet" and the child is "Industrial waste"
so I expected to see:
Planet - Industrial waste

Instead, I got:
Industrial waste - Planet
because it's an alphabetical order.

#573450

Hi, there's not a way to accomplish this in Views, but I have a custom shortcode that might work. Add this code to functions.php:

function list_hierarchical_terms($atts) {
    global $post;
    $taxonomy = $atts['taxonomy'];
    $separator = $atts['separator'];
    $terms = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
        $terms = trim( implode( ',', (array) $terms ), ' ,' );
        return wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms."&hierarchical=1"."&separator=".$separator."&echo=0&style=none");
         
    }
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');

Then you can use this shortcode instead of wpv-post-taxonomy:

[display_post_tax_terms_hierarchical taxonomy="category" separator=" - "]
#573526

It works perfectly
Thank you!

#574498

Hi,
I still have a question regarding this thread. Sorry.

How can I displayed the category with the format term names and not link?

I've tried to add format="name" to your code but it doesn't work.

Thank you!

#574526

If you just want the term name instead of a link, you should use this code instead:

function list_hierarchical_terms($atts) {
    global $post;
    $taxonomy = $atts['taxonomy']; // change this to your taxonomy
    $separator = '';
    $out = '';
    $terms = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "names" ) );
    foreach($terms as $term ) {
      $out .= $separator . $term;
      $separator = $atts['separator'];
    }
    return $out;
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');
#574533

Thanks, It displays names now but it's not hierachical anymore :-/

#574534

OK. I found a solution! Thanks

#574542

Okay great, I was just about to recommend this change:

function list_hierarchical_terms($atts) {
    global $post;
    $taxonomy = $atts['taxonomy']; // change this to your taxonomy
    $separator = '';
    $out = '';
    $term_ids = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    usort( $term_ids, 'cmp' );
    foreach($term_ids as $term_id ) {
      $term = get_term( $term_id, $taxonomy, OBJECT );
      $out .= $separator . $term->name;
      $separator = $atts['separator'];
    }
    return $out;
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.