To display posts from a specific category in WordPress, you can use the WP_Query
class along with the cat
parameter. This allows you to retrieve posts based on their assigned category.
Here’s an example code snippet that demonstrates how to display posts from a specific category:
$args = array(
'cat' => 5, // Replace 5 with the ID of the category you want to display posts from
'posts_per_page' => 10, // Number of posts to display
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Display post content or any other desired information
the_title();
the_content();
}
} else {
// No posts found
echo 'No posts found.';
}
// Restore original post data
wp_reset_postdata();
In this example, we define the $args
array with the cat
parameter set to the ID of the category you want to display posts from. You can find the category ID by going to the WordPress dashboard and navigating to Posts > Categories. Hover over the category name and the ID will be displayed in the URL.
The posts_per_page
parameter is set to limit the number of posts displayed. You can adjust this value as per your requirements.
We then create a new instance of WP_Query
with the $args
array as the parameter. The WP_Query
class allows us to query posts based on various parameters.
Next, we check if there are any posts returned by the query using the have_posts()
method. If there are posts, we loop through them using the while
loop and use the the_post()
function to set up the post data for each iteration.
Inside the loop, you can access and display the post content using functions like the_title()
and the_content()
.
Finally, we use wp_reset_postdata()
to restore the original post data, ensuring that other functions outside of this query work correctly.
This code snippet can be useful when you want to create a custom page template or widget that displays posts from a specific category on your WordPress site.
Examples
Example 1: Displaying posts from a specific category on a page
This use case demonstrates how to display posts from a specific category on a WordPress page using a custom query. The code example below retrieves posts from the “News” category and displays their titles and excerpts.
<?php
$args = array(
'category_name' => 'news',
'posts_per_page' => 5,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_title( '<h2>', '</h2>' );
the_excerpt();
}
wp_reset_postdata();
} else {
echo 'No posts found.';
}
?>
The code uses the WP_Query
class to create a custom query that retrieves posts from the “News” category. The category_name
parameter is set to “news” to specify the category. The posts_per_page
parameter limits the number of posts displayed to 5. Inside the loop, the the_title()
function is used to display the post title wrapped in <h2>
tags, and the the_excerpt()
function is used to display the post excerpt.
Example 2: Displaying posts from a specific category in a widget
This use case demonstrates how to create a custom widget that displays posts from a specific category in a sidebar. The code example below creates a widget that retrieves and displays the titles of posts from the “Featured” category.
<?php
class WPSE_Custom_Category_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'wpse_custom_category_widget',
'Custom Category Widget',
array( 'description' => 'Displays posts from a specific category.' )
);
}
public function widget( $args, $instance ) {
$category = 'featured';
$posts_per_page = 5;
$args = array(
'category_name' => $category,
'posts_per_page' => $posts_per_page,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo $args['before_widget'];
while ( $query->have_posts() ) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
}
echo $args['after_widget'];
wp_reset_postdata();
}
}
}
function wpsnippets_register_custom_widget() {
register_widget( 'WPSE_Custom_Category_Widget' );
}
add_action( 'widgets_init', 'wpsnippets_register_custom_widget' );
?>
The code defines a custom widget class WPSE_Custom_Category_Widget
that extends the WP_Widget
class. The widget()
method is responsible for displaying the widget’s content. Inside the method, the category name and the number of posts per page are set. The WP_Query
class is used to retrieve posts from the specified category, and the post titles are displayed using the get_the_title()
function.
Example 3: Displaying posts from multiple categories on a page
This use case demonstrates how to display posts from multiple categories on a WordPress page using a custom query. The code example below retrieves posts from the “News” and “Events” categories and displays their titles and excerpts.
<?php
$args = array(
'category_name' => 'news,events',
'posts_per_page' => 10,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_title( '<h2>', '</h2>' );
the_excerpt();
}
wp_reset_postdata();
} else {
echo 'No posts found.';
}
?>
The code uses the WP_Query
class to create a custom query that retrieves posts from both the “News” and “Events” categories. The category_name
parameter is set to “news,events” to specify multiple categories. The posts_per_page
parameter limits the number of posts displayed to 10. Inside the loop, the the_title()
function is used to display the post title wrapped in <h2>
tags, and the the_excerpt()
function is used to display the post excerpt.