0

I know that there are a few ways of doing this but for everything that I have tried I can only manage to get the first category. For example:

<?php echo get_the_category_list(); ?>

Only shows one. Like:

<?php 
    foreach((get_the_category()) as $category){
        echo $category->name."<br>";
        echo category_description($category);
        }
 ?>

Shouldn't this functions get me the full list of existing categories?

3 Answers 3

3

It should be noted that both

<?php echo get_the_category_list(); ?>

and

<?php 
    foreach((get_the_category()) as $category){
        echo $category->name."<br>";
        echo category_description($category);
        }
 ?>

display ALL categories that are assigned to the current post in the loop.

From your question's title, I understand that you want to display all available categories that exist in the website, so wp_list_categories() is more suitable. So using:

<ul>
    <?php wp_list_categories(); ?> 
</ul>  

will return a list of all categories that have been assigned to at least one post. You can see the documentation for the function here.

1
  • Yep, that was my issue, thanks for the explanation.
    – Sergi
    Commented Oct 19, 2017 at 18:08
0

This code should work. Please check if you have post assigned to your categories. Category should not be displayed if empty.

1
  • wp_list_categories(); did the trick but I don't understand why the category list wouldn't work.
    – Sergi
    Commented Oct 19, 2017 at 11:23
0

In my case, this worked and displayed all of the categories:

wp_list_categories();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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