How to display WordPress Categories on front page

Given are 5 categories & their sub-categories in my WordPress Website. Sub-categories contains posts. I want to display categories (Only categories) on my front page. When a user click on any category, I want to show it’s all sub-categories on another page and when user click on any sub-category, I want to display all the posts that category contains.

Please tell me how to do it? I have studied WordPress category guides but did not get any clue.

Related posts

1 comment

  1. Add the following code to your front page template:

    wp_list_categories();

    That will print out a list of all your categories. You can then hide the subcategories if you wish using CSS.

    By default, clicking a category link will then open the category page, which uses the category.php template, and this will show all the posts in that category.

    More information here: https://codex.wordpress.org/Template_Tags/wp_list_categories

    To list the subcategories of a particular category:

    $category_id = get_cat_ID('Category Name');
    $categories = get_categories(array('child_of' => $category_id));
    foreach($categories as $category) { 
        echo '<a href="' . get_category_link( $category->term_id ) . '">' .       $category->name.'</a><br> ';
    }
    

Comments are closed.