How to Display Posts from a Category on a Page in WordPress

Home » Snippets » How to Display Posts from a Category on a Page 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

Do you want to display specific posts from a category on a page of your WordPress website? WordPress provides a powerful feature that allows you to organize your posts into categories, making it easier for your visitors to find and browse related content. In this article, we’ll show you how to properly display posts from a specific category on a page using WordPress. Whether you’re creating a blog page or a custom landing page, this tutorial will guide you through the step-by-step process.

					function wpturbo_display_category_posts( $category_slug ) {
    $args = array(
        'post_type' => 'post',
        'cat' => get_category_by_slug( $category_slug )->term_id,
        'posts_per_page' => 5,
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        echo '<ul>';
        while ( $query->have_posts() ) {
            $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }
        echo '</ul>';
        wp_reset_postdata();
    } else {
        echo '<p>No posts found.</p>';
    }
}

wpturbo_display_category_posts( 'news' );
				

The code snippet provided is used to display posts from a specific category on a page. This can be useful if you want to create a page dedicated to displaying posts from a particular category.

The first part of the code snippet is the function declaration: wpturbo_display_category_posts(). This function takes a parameter $category_slug, which is the slug of the category you want to display posts from.

Inside the function, we define an array variable called $args which contains the arguments for our query. The 'post_type' parameter is set to 'post', indicating that we want to query regular blog posts. The 'cat' parameter is set to the term ID of the category, obtained using the get_category_by_slug() function. The 'posts_per_page' parameter is set to 5, which means we want to display a maximum of 5 posts from the category.

Next, we create a new instance of the WP_Query class, passing in the $args array as the argument. This will execute the query and return the posts that match our criteria.

We then start a conditional statement with if ( $query->have_posts() ). This checks if there are any posts returned by the query. If there are, we enter the if block.

Inside the if block, we start by echoing an opening <ul> tag, indicating the start of an unordered list. Then, we use a while loop to iterate through each post returned by the query. We use the the_post() function to set up the current post for display. Inside the loop, we echo an <li> tag with an anchor link (<a>) that displays the post title and links to the individual post (get_permalink() and get_the_title() functions are used here).

After the loop, we echo a closing </ul> tag to end the unordered list. Finally, we call wp_reset_postdata() to reset the global post data back to the main query, ensuring that subsequent functions outside of this loop work correctly.

If the query does not find any posts in the specified category, we enter the else block and echo a simple message saying "No posts found."

To use this code snippet, you can call the wpturbo_display_category_posts() function and pass in the desired category slug as a parameter. In the example provided, the category slug ‘news’ is passed to the function, so it will display posts from the ‘news’ category.

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