Add Snippet To Project
Are you looking for a way to enhance the user experience on your WordPress site by displaying related posts from the same category? By showing users more content that they might be interested in, you can increase engagement and keep visitors on your site longer. In this article, we’ll guide you through the process of displaying other posts from the same category on your WordPress site, helping you take your website’s navigation and content organization to the next level.
function wpturbo_display_other_posts_same_category() {
$current_post_id = get_the_ID();
$current_post_categories = wp_get_post_categories($current_post_id);
if (!empty($current_post_categories)) {
$args = array(
'category__in' => $current_post_categories,
'post__not_in' => array($current_post_id),
'posts_per_page' => 3,
'ignore_sticky_posts' => 1
);
$related_posts = new WP_Query($args);
if ($related_posts->have_posts()) {
echo '<ul>';
while ($related_posts->have_posts()) {
$related_posts->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
wp_reset_postdata();
} else {
echo 'No related posts found.';
}
} else {
echo 'No categories found for the current post.';
}
}
add_action('wpturbo_display_other_posts_same_category', 'wpturbo_display_other_posts_same_category');
The purpose of this code snippet is to display a list of other posts from the same category as the current post. This can be useful to provide additional content recommendations to users and encourage them to explore more related content on your website.
First, we define a function called wpturbo_display_other_posts_same_category()
, which will be responsible for retrieving and displaying the related posts. Inside this function, we perform the following steps:
-
We use the
get_the_ID()
function to retrieve the ID of the current post, which we store in a variable called$current_post_id
. -
We use the
wp_get_post_categories()
function to get an array of category IDs that the current post belongs to. This is stored in the variable$current_post_categories
. We check if the array is not empty to ensure that the current post has categories assigned to it. -
If the current post has categories assigned, we define an array called
$args
with the parameters for our query to retrieve related posts. These parameters are:category__in
: This specifies the category IDs that we want to include in the query. We pass in the$current_post_categories
array to include posts from the same categories.post__not_in
: This ensures that the current post is excluded from the query by passing its ID in an array.posts_per_page
: This limits the number of related posts to be displayed, in this case, set to 3.ignore_sticky_posts
: This ensures that any sticky posts (if present) are not included in the query.
-
We create a new instance of the
WP_Query
class and pass in the$args
array to retrieve the related posts. We store the query results in a variable called$related_posts
. -
We check if
$related_posts
has any posts using thehave_posts()
method. If there are related posts, we start an unordered list (<ul>
) to display the post titles. -
Within a while loop, we call the
the_post()
method to set up the data for the next post in the$related_posts
query. This allows us to retrieve the title of each related post using theget_the_title()
function. We echo the title within a list item (<li>
) for each related post. -
After the loop, we close the unordered list (
</ul>
) and then use thewp_reset_postdata()
function to restore the original post data. -
If
$related_posts
does not have any posts, we echo the message "No related posts found." -
If the current post does not have any categories assigned to it, we echo the message "No categories found for the current post."
Finally, we hook the wpturbo_display_other_posts_same_category()
function into the wpturbo_display_other_posts_same_category
action. This ensures that the function is called at the appropriate time to display the related posts.