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 ).

Related :   How to add WordPress Follow Button for Your Website ?

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 ?”


lynxbee_ezoic

Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


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

Leave a Comment

Android Android Applications Android Build system Android Commands Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded