Related posts in WordPress is a list of related posts to each other that the theme will display after the post’s content and is a great way to increase user engagement on your website or blog. By displaying related posts on your site, you can keep visitors on your website longer, increase page views, and encourage them to explore more content. This list of posts is related to the current post by category in WordPress.
One of the most popular methods for displaying related posts is to use a plugin. There are many plugins available that allow you to easily add related posts to your website. These plugins give you the ability to customize the display of the related posts, such as the number of posts to display, the title of the related posts, and the images associated with the posts.
Another popular method for displaying related posts is to manually code the related posts into the template of your WordPress site. This method is more time-consuming but allows you to customize the display of the related posts more precisely.
When displaying related posts, it is important to choose posts that are related to the current post. You can choose related posts based on categories, tags, or other criteria. It is also important to choose posts that are recent and relevant. This will ensure that the related posts are more likely to be interesting to the reader.
To add related posts to your WordPress theme, open the functions.php
file and add the following code.
function hs_related_posts()
{
$categories = get_the_category();
if (count($categories) > 0) :
?>
<?php
$terms = get_the_terms(get_the_ID(), 'category');
if (empty($terms)) $terms = array();
$term_list = wp_list_pluck($terms, 'slug');
$related_args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'post_status' => 'publish',
'post__not_in' => array(get_the_ID()),
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term_list
)
)
);
$the_query = new WP_Query($related_args);
global $cl_from_element;
$cl_from_element['is_related'] = true;
// Display posts
if ($the_query->have_posts()) :
?>
<div class="related-posts">
<h2 class="widget-title">
<?php esc_html_e('Related Posts', 'text-domain') ?>
</h2>
<div class="posts">
<?php $counter = 1;
while ($the_query->have_posts() && $counter < 4) :
$the_query->the_post(); ?>
<div class="related-post">
<a href="<?php echo esc_url(get_permalink()); ?>">
<?php if (has_post_thumbnail()) : ?>
<div class="related-post-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
</a>
<?php the_title(sprintf('<h2 class="related-post-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h2>'); ?>
</div>
<?php
$counter++;
endwhile;
?>
</div>
</div>
<?php endif;
wp_reset_query();
$cl_from_element['is_related'] = false;
endif;
}
Go to your posts display loop and add the function below in it.
hs_related_posts();
The best way to add related posts to your WordPress theme is to add them by the child theme.
When displaying related posts, it is important to make sure that the posts are displayed in an aesthetically pleasing way. This means that the posts should be displayed in a way that is visually appealing and does not take away from the content of the current post.