Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

PHPWordpress
Home›PHP›Add related posts in each posts in WordPress

Add related posts in each posts in WordPress

By SibeeshVenu
July 14, 2015
1028
0
Share:

Introduction

In this article we will learn how we will add related posts to each posts in wordpress according to the tags given. Even though there are so many plugins available to do the same, here I am going to show you a demo of implementing the same without a plugin. I hope you will like it.

Background

Today I was writing some articles in my website, once I finished my writing, I was just thinking about showing the related posts to each posts according to the tags selected. Hereby I will show you how we can implement it.

Before going through it, we can do this in two way.

  • With a plugin
  • Without a plugin
  • I will always recommend you to use normal PHP, WordPress implementation which does not require any plugin. Do you know why?

    Limitations if use plugins

  • It may change your theme style
  • It may not be supported your framework version
  • It may slow down your website
  • If you include a plugin, you are giving credits to the plugin author
  • Some plugins may have some internal redirects, it may cause unwanted loads
  • Some plugins add ads without your knowledge
  • These all problems will be resolved if you use normal few lines of PHP codes.

    If you still need to go for a plugin, you can see some plugins here: Related Posts Plugins

    Using the code

    Since we need that related posts entry must be seen in each and every posts, we should make some changes in the file called single.php. You can either edit this file or create a widget.

    Add a related posts in wordpress

    Add a related posts in wordpress

    Find out end of each posts, you can inspect one of your page elements in UI and find out the class name and search the same in the single.php file. For me it is class post-excerpt.

    Now after that class ends, you need to paste the below lines of codes

    [php]
    <div class="relatedposts">
    <h3>Related posts</h3>
    <?php
    $orig_post = $post;
    global $post;
    $tags = wp_get_post_tags($post->ID);

    if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    $args=array(
    ‘tag__in’ => $tag_ids,
    ‘post__not_in’ => array($post->ID),
    ‘posts_per_page’=>5, // Number of related posts to display.
    ‘caller_get_posts’=>1
    );

    $my_query = new wp_query( $args );

    while( $my_query->have_posts() ) {
    $my_query->the_post();
    ?>

    <div class="relatedthumb">
    <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
    <?php the_title(); ?>
    </a>
    </div>

    <? }
    }
    $post = $orig_post;
    wp_reset_query();
    ?>
    </div>
    [/php]

    You can set your posts count in the below line of code.

    [php]
    ‘posts_per_page’=>5
    [/php]

    Our next step is to add some css styles to the links.

    [css]
    <style>
    .relatedthumb a {
    color: #7a7a7a;
    text-decoration: none;
    }
    .relatedthumb a:hover {
    color: #01a821;
    }
    </style>
    [/css]

    You can either paste this CSS in single.php file or in style.css

    Now update your file, and run your website. Click on any post and see the output.

    I have selected a post which is related to JQuery, and got the related posts as follows. It works great.

    Add a related posts in wordpress

    Please be noted that you need to add tags in each posts, then only this mechanism works.

    Reference: Related Posts

    Conclusion

    That is all. I hope you liked this article. Please share me your feedback.

    Kindest Regards
    Sibeesh Venu

    TagsAdd a related posts in wordpressAdd related posts in wordpress without pluginadding posts related to tagsPHPrelated posts in wordpressWordpress
    Previous Article

    How to find PublicKeyToken of a DLL

    Next Article

    Million Billion Trillion Calculations

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • Install Wordpress Through Web Platform Installer
      Web Platform InstallerWebMatrixWordpress

      Use WordPress Without Wamp Server

      December 8, 2015
      By SibeeshVenu
    • Scratching_Head
      Wordpress

      Most Needed WordPress Plugins

      January 25, 2016
      By SibeeshVenu
    • Code SnippetsPHP

      How to use shortcode inside any php file

      July 17, 2015
      By SibeeshVenu
    • Adding end points in Azure Virtual Machine
      AzureDatabaseMySQLVirtual MachineWordpress

      Link Your MySQL Database To Azure Virtual Machine And WordPress

      September 24, 2015
      By SibeeshVenu
    • Wamp ServerWordpress

      Set up WordPress in Your Local Wamp Server

      April 29, 2015
      By SibeeshVenu
    • File Explorer
      Wordpress

      Briefly unavailable for scheduled maintenance Check back in a minute

      July 1, 2016
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version