Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom taxonomy(the taxonomy is types) that is basically a selector that tells WordPress which section of the site to place the new post.

So for example

  1. I create a new post and select Lifestyle from the custom taxonomy list.
  2. Then I would select a category to place the post under, for example Healthy

I am trying to list the categories of Lifestyle in a custom menu format but only if posts exist under the taxonomy type and category selected.

What I am getting is a full list of categories but not filtered by taxonomy type.

Can anyone see where I have gone wrong?

<?php
$args=array(
  'types' => 'Lifestyle',
  'orderby' => 'name',
  'order' => 'ASC',
  'hide_empty' => 1
  );
$categories=get_categories($args);
  foreach($categories as $category) {
    if ($category->count > 0){
    echo '<li class="'.$category->slug.'"><a href="?category=' . $category->slug . '">'.$category->name.' / '.$category->count.'</a></li>';
        }
    } 
?>

Thanks so much in advnce! -d

share|improve this question
add comment

1 Answer

get_categories() only lists categories, so you shouldn't be seeing any terms from the custom taxonomy. What you want to use is get_terms() which takes a taxonomy as an argument. As you can read about in this answer, get_terms() is actually used by get_categories(), it just prefills the "taxonomy" argument with "category."

If you swap in get_terms() and add the name of your custom taxonomy, your code looks like it should work. The only other thing you may be able to change is your if statement checking the category count. If you're already doing 'hide_empty' => 1 you shouldn't need that.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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