How to List All Categories with Posts in WordPress

Home » Snippets » How to List All Categories with Posts in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Managing a content-rich WordPress website with diverse categories can be a daunting task, especially when you need to keep a tab on which categories have posts and which ones do not. Thankfully, WordPress provides a simple way to view all the categories along with their respective posts. In this comprehensive guide, we will explore the process of listing all the categories with posts, allowing for easier and more efficient management of your content.

					function wpturbo_category_with_posts() {
    $categories = get_categories(array(
        'hide_empty' => 1
    ));

    foreach($categories as $category) {
        echo '<h2>' . $category->name . '</h2>';

        $posts = get_posts(array(
            'category' => $category->term_id
        ));

        foreach($posts as $post) {
            echo '<p><a href="' . get_permalink($post->ID) .'">' . $post->post_title.'</a><p>';
        }
    }
}
add_shortcode('wpturbo_category_with_posts', 'wpturbo_category_with_posts');
				

The given snippet of code is used to display all categories with their corresponding posts on a WordPress site. This function can be particularly beneficial for a blog website to neatly segment all posts under their respective categories.

The first part of the script defines a function called wpturbo_category_with_posts(). Inside this function, we use WordPress’s in-built get_categories() method to get an array of all the category objects that have at least one post associated with them. This is achieved by passing an associative array that specifies hide_empty option as 1.

$categories = get_categories(array('hide_empty' => 1));

Now that we have the categories, we loop over them using a foreach loop. Inside this loop, the name of each category is first printed as a header.

echo '<h2>' . $category->name . '</h2>';

We then fetch all posts associated with the current category using the get_posts() method, which we pass an associative array that specifies the ‘category’ option as the term_id of the current category. This gets us an array of all post objects associated with the category.

$posts = get_posts(array('category' => $category->term_id));

Next, we loop over each post object within the $posts array. For every post, we print a link to the post using the get_permalink() function, which will generate the proper URL for the post. We do this inside paragraph

tags that provide default styling to the links. We set the link text to the title of the post using the post_title object property of the $post object.

foreach($posts as $post) {
    echo '<p><a href="' . get_permalink($post->ID) .'">' . $post->post_title.'</a><p>';
}

Finally, the function wpturbo_category_with_posts() is registered as a shortcode using the add_shortcode() function. This lets you easily insert the function anywhere on your site by simply using the shortcode [wpturbo_category_with_posts].

add_shortcode('wpturbo_category_with_posts', 'wpturbo_category_with_posts');

This way, this function provides an easy way of displaying all categories with their associated posts in a structured way on your WordPress site.

Register an account to save your snippets or go Pro to get more features.