Skip to content
Menu
Quedank: Simplify
  • Home
  • Recent Posts
  • Computer Science
    • Servers and Hosting
    • Programming and Coding
    • Web
  • Digital Marketing
  • Multimedia
Quedank: Simplify
June 10, 2021February 10, 2022

Sample Code of a “Related Posts” Loop in WordPress. With Post Thumbnail, Category Name, and Post Author

-Advertisement-

For most publisher websites, it’s often a great idea to show your readers other posts that are related to the article they’re reading. A great way to do this is to add a “Related Posts” section at the bottom of the article that lists a few more related topics.

Some WordPress themes might not have this feature so it’s up to the developer to create this. Here’s the sample code of a “Related Posts” WordPress loop that you can copy. It’s usually common to place this after the post’s content.

<div id="related-posts" >
	<h2>You might also like</h2>
	<div class="inner-related-posts">
	<?php
		// args to get posts from the related category
		$feed_args = array(
                'category__in' => wp_get_post_categories( get_queried_object_id() ),
                'posts_per_page' => 7,
                'orderby'       => 'DESC',
                'post__not_in' => array( get_queried_object_id() )
                );
				
		$feed_query = new WP_Query( $feed_args );		

		if ( $feed_query->have_posts() ) : ?>

        <!-- the loop -->
        <?php while ( $feed_query->have_posts() ) : $feed_query->the_post(); 
			// Get the primary category name
			$feed_categories = get_the_category(); 
			$feed_cat_name = $feed_categories[0]->cat_name;
			
		?>

            <div class="card">
				<a class="card-img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);" href="<?php the_permalink(); ?>"></a>
				<div class="card-text">
					<div class="card-category"><?php echo $feed_cat_name;?></div>
					<h3 class="card-post-title"><a href="<?php the_permalink(); ?>"><?php echo get_the_title();?></a></h3>
					<div class="card-post-author">By <?php the_author();?></div>
				</div>
			</div>

        <?php endwhile; ?>
        <!-- end of the loop -->
        <?php wp_reset_postdata(); ?>
     <?php endif; ?>
	
	</div>
</div>

To add some style, you will need to include some CSS’s:

.card {
    padding: 10px 0;
    border-top: 1px solid #eee;
}
.card-img {
    display: inline-block;
    background-position: center center;
    background-size: cover;
    background-repeat: no-repeat;
    width: 38%;
    padding-top: 20%;
}
.card-text {
    display: inline-block;
    width: 55%;
    vertical-align: top;
    padding: 0 10px;
}
.card-category {
    font-size: 14px;
    text-transform: uppercase;
}
.card-post-title {
    margin-top: 2px;
    margin-bottom: 5px;
}
.card-post-author {
    font-size: 14px;
    color: #a9a9a9;
}

The end result would look like this. The sample loop calls more posts from the same category and lists them with thumbnail, title, and author name.

Sample “Related Posts” WordPress loop

Why is it good to have a “Related Posts” section?

It is a great idea to have a “Related Posts” section to entice your readers to stay on your website. The longer they stay in your website, the better your Bounce Rate and Average Time on Page scores will be in Google Analytics. Both are great indicators for search engines to feature your website to searchers. Thus, it can help with your website’s SEO ranking and trust rating.

-Advertisement-

Search A Topic

Recent Posts

  • Why Use Python Over R. And Why You Might Not Like R At All.
  • How To Fix “Warning: Creating default object from empty value in…” In WordPress
  • Fix WordPress “Warning: Cannot modify header information – headers already sent by…”
  • Solved: Changing the ‘Publish Date’ does not update post order in WordPress
  • How To Trim Or Shorten The Blank Ending Of The Timeline In DaVinci Resolve
  • Easiest way to redirect your visitors from your old WordPress website to your new. By using 301 Redirect plugins
  • How to export WordPress posts WITH their images and then import them to another website
  • Solved: “The server cannot process the image.” Error in WordPress When Uploading An Image
  • Reaching Out To Influencers For Brand Awareness, Plus A Sample Message Template
  • 3-Step Template To Create A Case Study That Closes Your Leads
©2023 Quedank: Simplify | All Rights Reserved | Privacy Policy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT