To display recent posts from a specific category in WordPress, you can use the WP_Query
class along with the category_name
parameter. This allows you to retrieve posts from a specific category and display them on your website.
Here’s an example code snippet that demonstrates how to achieve this:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5, // Number of posts to display
'category_name' => 'your-category-slug', // Replace with your category slug
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Display post title and content
the_title();
the_content();
}
} else {
// No posts found
echo 'No posts found.';
}
// Restore original post data
wp_reset_postdata();
?>
In this code snippet, we first define the arguments for the WP_Query
class. We set the post_type
parameter to 'post'
to retrieve only posts, and the posts_per_page
parameter to 5
to display a maximum of 5 posts. Replace 'your-category-slug'
with the slug of the category you want to display posts from.
Then, we create a new instance of WP_Query
with the defined arguments and check if there are any posts available using the have_posts()
method. If there are posts, we loop through them using the while
loop and use the the_title()
and the_content()
functions to display the post title and content.
If no posts are found, we display a message saying “No posts found.”
Finally, we use the wp_reset_postdata()
function to restore the original post data. This is important to ensure that other post-related functions work correctly after the custom query.
Examples
Example 1: Display recent posts from a specific category on a page
This use case demonstrates how to display a list of recent posts from a specific category on a WordPress page. The code example uses the WP_Query
class to retrieve the posts and the get_permalink()
and the_title()
functions to display the post links and titles.
<?php
$args = array(
'category_name' => 'news',
'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 'No posts found.';
}
?>
The code sets up a query to retrieve posts from the “news” category, limited to 5 posts per page. It then loops through the posts, displaying each post’s title and link within an unordered list. Finally, the wp_reset_postdata()
function is called to restore the global post data.
Example 2: Display recent posts from a specific category in a widget
This use case demonstrates how to create a custom widget that displays recent posts from a specific category. The code example uses the WP_Widget
class to create the widget and the WP_Query
class to retrieve the posts.
<?php
class WPSnippets_Recent_Posts_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'wpsnippets_recent_posts_widget',
'Recent Posts from Category',
array( 'description' => 'Displays recent posts from a specific category.' )
);
}
public function widget( $args, $instance ) {
$category = isset( $instance['category'] ) ? $instance['category'] : '';
$query_args = array(
'category_name' => $category,
'posts_per_page' => 5,
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
echo $args['before_widget'];
echo $args['before_title'] . 'Recent Posts' . $args['after_title'];
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
echo '</ul>';
echo $args['after_widget'];
wp_reset_postdata();
}
}
public function form( $instance ) {
$category = isset( $instance['category'] ) ? $instance['category'] : '';
?>
<p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>">Category:</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" type="text" value="<?php echo esc_attr( $category ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['category'] = ( ! empty( $new_instance['category'] ) ) ? sanitize_text_field( $new_instance['category'] ) : '';
return $instance;
}
}
function wpsnippets_register_recent_posts_widget() {
register_widget( 'WPSnippets_Recent_Posts_Widget' );
}
add_action( 'widgets_init', 'wpsnippets_register_recent_posts_widget' );
?>
The code defines a custom widget class WPSnippets_Recent_Posts_Widget
that extends the WP_Widget
class. The widget’s constructor sets the widget’s name and description. The widget()
method is responsible for displaying the widget’s content, including the recent posts from the specified category. The form()
method creates the widget’s settings form, allowing the user to specify the category. The update()
method handles the saving of the widget’s settings.
Example 3: Display recent posts from a specific category using a shortcode
This use case demonstrates how to create a shortcode that displays recent posts from a specific category. The code example uses the WP_Query
class to retrieve the posts and the get_permalink()
and the_title()
functions to display the post links and titles.
<?php
function wpsnippets_recent_posts_shortcode( $atts ) {
$atts = shortcode_atts( array(
'category' => 'news',
'posts_per_page' => 5,
), $atts );
$query_args = array(
'category_name' => $atts['category'],
'posts_per_page' => $atts['posts_per_page'],
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
$output = '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
$output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
$output .= '</ul>';
wp_reset_postdata();
} else {
$output = 'No posts found.';
}
return $output;
}
add_shortcode( 'recent_posts', 'wpsnippets_recent_posts_shortcode' );
?>
The code defines a shortcode function wpsnippets_recent_posts_shortcode()
that accepts attributes for the category and number of posts to display. It uses the shortcode_atts()
function to merge the provided attributes with default values. The function then sets up a query to retrieve posts from the specified category and loops through the posts, building an output string with the post links and titles. Finally, the function returns the output string, which will be displayed wherever the [recent_posts]
shortcode is used.