Support » Fixing WordPress » Code snippet: Show all category information for example on the front page

  • This is a code snippet that shows all the category information for example on the front page.

    It uses get_categories(). It will display the get_categories() array, as well as echo a ‘test’ value, and the value you wish to have shown. The code as shown should be used outside the loop.

    It can be handy when for example you are designing a theme, and you want to build a dynamic code part.

    Do not paste the code snippet below in a live website.

    <?php 
    
         $categories = get_categories(); 
    
        	 	print_r($categories);
    
         foreach ($categories as $category) {
    
        		echo 'test<br />';
    
         		echo $category->category_parent . '<br />';
    
         }
    
        ?>

    The example shows the ids’ of all of the parent categories.

    You can substitute this with any other array field available in the get_categories() function.

    For example:

    echo $category->category_parent

    to:

    echo $category->cat_name

    Which will then display all the category names.

    Depending on your needs you can choose to build from there.

  • The topic ‘Code snippet: Show all category information for example on the front page’ is closed to new replies.