Home » Web Design and Development » Content Management System » Wordpress » How to display WordPress Categories on home page of website ?

How to display WordPress Categories on home page of website ?

If we have a website which is largely text based, something like this website which shows mostly text posts then its always better to have a home page which lists all the categories so user can choose the category they want and we display the posts under that category on next category pages.

For getting the list of categories, wordpress provides an API “wp_list_categories” using which we can get the list of all categories. This API also accepts an arguments which can be used to customise look and features of category listing. To add categories on home page modify your wordpress based websites source code as below,

<?php if ( is_home() || is_front_page() ) : ?>
    <?php wp_list_categories(array(
        'hierarchical'        => false,
        'style'               => 'list',
        'separator'           => '<hr>',
        'depth'               => '2',
        'order'               => 'ASC',
        'orderby'             => 'name',
        'show_count' => true
    ) ); ?>
<?php endif; ?>

details of more supported arguments for “wp_list_categories” can be found at wordpress website ( Link ).

As an example, we will show here how we added this to our wordpress based website’s home page,

$ vim wp-content/themes/mywebsite_theme/page.php

opened page.php from our website’s theme and pasted below code,

<div class="single_page">
    <header>
        <h1 class="title entry-title"><?php the_title(); ?></h1>
    </header>

   <?php if ((is_home() || is_front_page()) && !is_tag()) : ?>
<?php wp_list_categories(array(
        'hierarchical'        => false,
        'style'               => 'list',
        'separator'           => '<hr>',
        'depth'               => '2',
        'order'               => 'ASC',
        'orderby'             => 'name'
    ) ); ?>
    <?php endif; ?>
</div>

If you want to display subcategories on navigation pages, you can visit our post “Display all subcategories from a specific category on selected Category Archive page ?”

2 thoughts on “How to display WordPress Categories on home page of website ?”

Leave a Comment