Add a related-post section to a classic WordPress theme by defining a relevance rule—such as shared categories—then running a secondary query and linking each result’s title and featured image to its post. Use get_posts() or a separate WP_Query, not query_posts(); the latter replaces the main query rather than serving as a safe secondary-listing tool.
Before you add the related-post section
This pattern is for a classic theme with editable PHP templates, typically the single-post template. Back up the theme and make changes in a child theme where possible, so an update to the parent theme does not overwrite them. The exact template and styling depend on the active theme.
Confirm featured-image support
The theme needs to declare add_theme_support( 'post-thumbnails' ) for WordPress’s featured-image interface to appear. WordPress recommends registering theme support in a setup callback on after_setup_theme, before init. See the add_theme_support() reference and the Featured Images & Post Thumbnails handbook page.
If you control the theme, a typical setup function looks like this:
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstall#1 Best Overall
function mytheme_setup() {
add_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'mytheme_setup' );
Do not add this blindly if the theme already declares support; first check its setup code.
Choose what “related” means
WordPress does not decide which posts are editorially related. Choose a rule that matches how the site is organized. A shared category is a straightforward starting point; shared tags or a custom taxonomy can produce a narrower set. Query functions retrieve posts that match criteria, but the relevance rule remains your choice. See the get_posts() reference for available query arguments.
| Rule | Useful when | Trade-off |
|---|---|---|
| Shared category | Categories describe broad subjects and posts are consistently assigned to them. | Broad categories can return loosely related posts. |
| Shared tag | Tags are applied consistently to identify specific topics. | Inconsistent or overly broad tagging can weaken relevance. |
| Custom taxonomy | The site has a purpose-built classification for a particular content relationship. | It requires that taxonomy to be registered and used consistently. |
Add a related-post list with get_posts()
For a compact list, get_posts() returns an array of matching posts without replacing the main query. In the following example, related posts share at least one category with the current post. Place the section in the single-post template where it should appear, commonly after the post content. Adjust the count and image size to suit the design.
<?php
$current_post_id = get_the_ID();
$category_ids = wp_get_post_categories( $current_post_id );
if ( $category_ids ) {
$related_posts = get_posts( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4,
'post__not_in' => array( $current_post_id ),
'category__in' => $category_ids,
'ignore_sticky_posts' => true,
) );
if ( $related_posts ) {
echo '<section class="related-posts" aria-labelledby="related-posts-title">';
echo '<h2 id="related-posts-title">Related posts</h2>';
echo '<ul>';
foreach ( $related_posts as $related_post ) {
$post_thumbnail = get_the_post_thumbnail(
$related_post->ID,
'medium'
);
if ( ! $post_thumbnail ) {
continue;
}
echo '<li>';
echo '<a href="' . esc_url( get_permalink( $related_post->ID ) ) . '">';
echo $post_thumbnail;
echo '<span>' . esc_html( get_the_title( $related_post->ID ) ) . '</span>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
echo '</section>';
}
}
?>
This example excludes the current post and limits the query to published standard posts. It also omits results without a featured image, so every displayed item has a thumbnail. If you want image-less posts included, remove the if ( ! $post_thumbnail ) check and render a designed fallback instead. The WordPress reference for get_the_post_thumbnail() documents output for a specified post and image size.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Rank #3
Choose the query style that fits the template
Use get_posts() when a simple array and explicit rendering are convenient. A separate WP_Query is also appropriate, particularly when you want to use WordPress’s loop methods for template output. WordPress explicitly recommends these two approaches for secondary listings, including related posts: query_posts() – Function.
With a custom WP_Query, keep the secondary loop separate from the main post loop and restore post data after it finishes:
Rank #4
<?php
$related_query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4,
'post__not_in' => array( get_the_ID() ),
'category__in' => wp_get_post_categories( get_the_ID() ),
'ignore_sticky_posts' => true,
) );
if ( $related_query->have_posts() ) :
echo '<section class="related-posts">';
echo '<h2>Related posts</h2>';
echo '<ul>';
while ( $related_query->have_posts() ) :
$related_query->the_post();
if ( ! has_post_thumbnail() ) {
continue;
}
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'medium' ); ?>
<span><?php the_title(); ?></span>
</a>
</li>
<?php
endwhile;
echo '</ul>';
echo '</section>';
endif;
wp_reset_postdata();
?>
Do not use query_posts() to create this feature. It alters the main query and can interfere with the page’s existing loop and pagination; a secondary listing should leave that query intact.
Pick an image size and handle missing thumbnails
Use a registered image size that fits the card rather than serving a full-size image by default. WordPress documents these default maximum dimensions: thumbnail 150 × 150 pixels, medium 300 × 300 pixels, medium_large up to 768 pixels wide with no height limit, and large 1024 × 1024 pixels. These are defaults and can be changed in Media settings. The Featured Images & Post Thumbnails documentation describes registered sizes and thumbnail output.
Recommended Free Tools
Best Value
For a consistent card grid, decide whether image-less posts should be omitted or shown with a fallback image. The examples omit them. If you register a custom image size for the cards, note that doing so does not automatically generate that size for existing featured images; regenerate image derivatives using an appropriate site workflow if needed. See set_post_thumbnail_size() for the standard thumbnail-size function and the handbook page for custom image sizes.
Style and verify the section
Add CSS in the theme’s stylesheet or your site’s established styling location. The markup uses a related-posts section and list so it can be styled without changing the query logic. For example, a grid can be created with CSS on .related-posts ul, while image dimensions and cropping can be controlled with .related-posts img. Avoid hard-coding dimensions that distort the chosen image ratio.
- Open posts that have several matching categories, only one match, and no matching posts; confirm the section behaves sensibly in each case.
- Check that the current post never appears in its own list.
- Test related posts both with and without featured images, according to the fallback policy you chose.
- Confirm titles and images both link to the correct post and that the image size suits the rendered layout.
- Verify that the theme’s main content, navigation, and pagination still behave as expected.
This is a template pattern, not a universally drop-in snippet: adapt the post type, taxonomy, placement, markup, and styling to the active theme and site structure.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minute




