PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] How to use get_the_category() and display the categories in menu order?

 January 17, 2022     php, wordpress     No comments   

Issue

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;
        }
    }

Solution

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.



Answered By - Stender
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing