I have one small problem, i am showing the custom fields of category on category page. its showing fine. but its not showing if there is no post in that category.

i don't know where to use the 'hide_empty' => 0 in my code.

here is the code which i used to show the fields of category.

$terms = get_the_terms( get_the_ID(), 'category');

if( !empty($terms) ) {

$term = array_pop($terms);
$expert_photo = get_field('expert_photo',$term);
$expert_name = get_field('expert_name',$term);
$expert_title = get_field('expert_title',$term);
$expert_description = get_field('expert_description',$term);
$expert_introduction = get_field('expert_introduction',$term);
  • You want to display all the terms? Regardless of current post? Use get_terms()
    – Sumit
    Mar 31 '16 at 5:00

You need to use wp_get_object_terms(). The function will get all the terms available in taxonomies based on the arguments supplied.

wp_get_object_terms( get_the_ID(), 'category', array( 'fields' => 'slug' ) );

For more info can be found in here

  • And how it will display the terms that is not in post ?
    – Sumit
    Mar 31 '16 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.