how to display all post category list in WordPress?

how to display all post category list in WordPress?

If you want to display post category list in WordPress site then this post is for you. As you know when your blog content grows, it’s helpful to know different methods for featuring categories on your blog. One of the most popular ways of featuring categories is by adding the list of categories in your blog sidebar.

Categories allow you to easily sort blogs on your website. They also help user to easily find there required content and these are also good for SEO of your website. Visitors can either select blog right from the home page or refine their search by clicking through to a blog category list. In this blog i will show you how to display category list of your blogs easily in WordPress.

In the WordPress there are many pre-defined functions to get all the categories. Like get_categories(), wp_list_categories() and wp_dropdown_categories(). These functions return the different type of output.

These WordPress functions accept a list of optional parameters to filter categories. These parameter lists can be an array or a query string. In this tutorial, we are going to see the uses of these functions and the usage procedure.

Option 1: get_categories()

This get_categories() function returns a list of category objects. From this function, we can retrieve categories to create our own html format to show category list. For more information go to this page get_categories().

<?php
$categories = get_categories( array(
    'orderby'       => 'name', //Defaults to 'date'
    'order'         => 'ASC', //Defaults to 'DESC'.
    'hide_empty'    => false, //if category is empty then show or not. Defaults to 'true'
));
?>
<ul class="blog-category">
    <?php
    foreach( $categories as $category ) {
        echo '<li class="category-item"><a class="category-link" href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></li>'; 
    } 
    ?>
</ul>

Output:-
get categories output

Option 2 : wp_list_catergories()

The wp_list_categories() function returns the pre-built HTML list of categories to display category list. For more information go to this page wp_list_catergories().

<?php
$categories = wp_list_categories( array(
    'title_li'      => '<h2>' . __( 'Categories', 'textdomain' ) . '</h2>',
    'show_count'    => true, // if show count or not. Defaults to 'false'
    'style'         => 'list', // style used to display the categories list. Defaults to 'list'
    'hide_empty'    => false, // if category is empty then show or not. Defaults to 'true'
    'taxonomy'      => 'category', // Name of the taxonomy to retrieve. Default 'category'
));

echo $categories;
?>

Output:-
wp list catergories output

Option 3: wp_dropdown_categories()

This function return post category list as a HTML dropdown list. you can use this function to show the dropdown of categories in any form you want. For more information go to this page wp_dropdown_categories().

<?php
wp_dropdown_categories( array(
    'hierarchical'  => true, // Whether to traverse the taxonomy hierarchy. Default 'false'
    'name'          => 'cat', // Value for the 'name' attribute of the select element. Default 'cat'.
    'id'            => 'cat_id', // Value for the 'id' attribute of the select element. Default $name.
    'class'         => 'cat-class', // Value for the 'class' attribute of the select element. Default 'postform'.
    'value_field'   => 'slug', // Term field that should be used to populate the 'value' attribute of the option elements. Accepts any valid term field: 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count'. Default 'term_id'.
    'taxonomy'      => 'category', // Name of the taxonomy to retrieve. Default 'category'
    'show_count'    => true, // if show count or not. Defaults to 'false'
    'style'         => 'list', // style used to display the categories list. Defaults to 'list'
    'hide_empty'    => false, // if category is empty then show or not. Defaults to 'true'
));
?>

Output:-
wp dropdown categories output

That’s it. if you want to show category list in any of your page or sidebar then paste this code in your functions.php file in a shortcode.

Leave a Reply

Your email address will not be published. Required fields are marked *

The link has been Copied to clipboard!