Home > Back-end >  How to use get_the_category() and display the categories in menu order?
How to use get_the_category() and display the categories in menu order?

Time:11-11

I'm using the below code to show the categories of the current post (excluding cat id 14). Issue is they're not in the order I want. I need them to display in menu order.. Is this possible?

  $categories = get_the_category();
    foreach($categories as $category) {
        $output = $category->cat_name;
        if($category->cat_ID !== 14){
            echo $output;
        }
    }

CodePudding user response:

After discussion in comments :

get_the_category() doesn't accept orderby clauses, so use wp_get_post_terms() instead.

Use it like this

$terms = wp_get_post_terms( get_the_id(), 'category', array( 'orderby' => 'term_order' , 'exclude' => array(14)));

This way the term with ID 14 is excluded as well.

  • Related