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 want to get all categories in a new page which doesn't include any post. I tried wp_list_categories(); function. But it show only "No category" message. How do I do it?

share|improve this question
    
There's an entire (wonderful!) site for these questions: wordpress.stackexchange.com –  editor Feb 24 '11 at 21:45
add comment

migrated from stackoverflow.com Feb 24 '11 at 23:08

This question came from our site for professional and enthusiast programmers.

2 Answers

This will only display the categories if there are posts associated with them, make sure that there are posts in your WordPress database. However, you might try:

wp_list_categories(array('hide_empty'=>0));

which will override that functionality.

share|improve this answer
add comment

This code will show and unordered list with the name of the category and the count the total posts containing.

<ul class="myClass">
 <?php $categories = get_categories(array('hide_empty'=>0,'number=100'));
       foreach ($categories as $cat) {
       echo "<li>". $cat->cat_name . " - ". $cat->category_count ." posts.</li>";
       } ?>
</ul>

Retrieves the 100 first categories (you can change the 100 for any number you want)

$categories = get_categories(array('hide_empty'=>0,'number=100'));

and for each of them (even the empty ones) shows cat_name and category_count

Copy the code above in the page file you want to dispay the result.

You will get some like this:

  • PHP - 0 posts.
  • Uncategorized - 4 posts.
  • WordPress - 1 posts.

I hope that could help you.

ps: If you need them as links, replace the line

 echo "<li>". $cat->cat_name . " - ". $cat->category_count ." posts.</li>";

by

echo '<li><a href="'.get_category_link($cat->cat_ID).'">'. $cat->cat_name . '</a> - '. $cat->category_count .' posts.</li>';
share|improve this answer
add comment

Your Answer

 
discard

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