How to Display a List of Posts from Multiple Categories in WordPress

Sometimes you may want to display a list of posts from multiple categories on your WordPress site. This can be useful for highlighting content from different blog sections or grouping related commands together.

This tutorial will show you how to display a list of posts from multiple categories in WordPress using a shortcode. This will allow you to easily insert the list into any post or page on your site.

Step 1: Create the Shortcode Function

The first step is to create a function for the shortcode. This function will build a query to retrieve the posts from the specified categories, then loop through the bars to generate the HTML output.

Paste the following code into your theme’s functions.php file:

function category_posts_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'categories' => '',  // A comma-separated list of category slugs
        'number' => 5,  // The number of posts to display
    ), $atts );

    // Convert the category slugs to an array of IDs
    $cat_slugs = explode( ',', $atts['categories'] );
    $cat_ids = array();
    foreach ( $cat_slugs as $cat_slug ) {
        $cat_slug = trim( $cat_slug );
        $cat = get_category_by_slug( $cat_slug );
        if ( $cat ) {
            $cat_ids[] = $cat->term_id;
        }
    }

    // Build the query arguments
    $args = array(
        'category__in' => $cat_ids,  // Include posts from any of the specified categories
        'post_type' => 'post',
        'posts_per_page' => $atts['number'],
    );
    $query = new WP_Query( $args );

    // Check if the query has posts
    if ( ! $query->have_posts() ) {
        return '<p>No posts found.</p>';
    }

    // Start the output
    $output = '<ul>';

    // Loop through the posts
    while ( $query->have_posts() ) {
        $query->the_post();
        $output .= '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
    }

    // Reset the post data
    wp_reset_postdata();

    // End the output
    $output .= '</ul>';

    return $output;
}
add_shortcode( 'category_posts', 'category_posts_shortcode' );

This function includes two shortcode attributes: “categories” and “numbers.” The “categories” attribute is a comma-separated list of category slugs, and the “number” attribute is the number of posts to show. The default values for these attributes are an empty string for the categories and 5 for the number of seats.

Step 2: Use the Shortcode in a Post or Page

To use the shortcode, type the below shortcode into a post or page, where “featured” and “news” are the slugs of the categories you want to display. You can also specify the number of posts to display by adding an attribute to the shortcode like this:

[category _posts categories="featured, news" number="10"]
[category _posts categories="featured, news"]

Just remove the space between these two words (category _posts). That’s it! You should now see a list of posts from the specified categories in your post or page.

Conclusion

Displaying a list of posts from multiple categories in WordPress is easy using a shortcode. With just a few lines of code, you can insert this list into any post or page on your site, giving you the flexibility to highlight specific content or group related posts together.

I hope this tutorial has helped you learn how to display a list of posts from multiple categories in WordPress using a shortcode. If you have any questions or need further assistance, please don’t hesitate to ask.

Good luck, and happy blogging!

Was this guide helpful?
YesNo