WordPress.org

Ready to get started?Download WordPress

Forums

Code snippet: Show all category information for example on the front page (1 post)

  1. design_dolphin
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 4 years ago by design_dolphin
  • This topic is not a support question
  • WordPress version: 2.8.5