Theme Layout

Theme Translation

Trending Posts Display

Home Layout Display

Posts Title Display


We Are Sorry, Page Not Found

Home Page
One of the reason that makes your Wordpress blog takes long time to load is too many plugin installed in your dashboard, so it will better to have minimum plugin possible to reduce loading times, and one of which is by using PHP code to display related post instead of using related post plugin.

And the following are 2 methods to display related post based on category and tag, and I will firstly warn you to back up your file before start doing anything to your Single.php file, or else you’ll be sorry if anything goes wrong, ok let’s get started.

1. Displaying related post based on Category (Categories):

Login to your Wordpress dashboard , go to your Appearance-> Editor and open your Single.php file, and add this code beneath your post content, like after this tag for example: <?php the_content() ?>:

    $this_post = $post;
    $category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
    $posts = get_posts('numberposts=6&offset=0&orderby=post_date&order=DESC&category='.$category);
    $count = 0;
    foreach ( $posts as $post ) {
    if ( $post->ID == $this_post->ID || $count == 5) {
    $count ++;
    <?php if ( $posts ) : ?>
    <div class="related_articles">
    <h2>Related Posts</h2>
    <?php foreach ( $posts as $post ) : ?>
    <li><a href="<?php the_permalink() ?>" title="<?php echo trim(str_replace("n"," ",preg_replace('#<[^>]*?>#si','',get_the_excerpt()))) ?>"><?php if ( get_the_title() ){ the_title(); }else{ echo "Untitled"; } ?></a> (<?php the_time('F jS, Y') ?>)</li>
    <?php endforeach // $posts as $post ?>
    <?php endif // $posts ?>
    $post = $this_post;

2. Displaying related post based on Tag (Tags):

Do exactly the same as the above steps and add this PHP code inside your Single.php file:
$tags = wp_get_post_tags($post->ID);
if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'showposts'=>5, // Number of related posts that will be shown.
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ) {
        echo '<h3>Related Posts</h3><ul>';
        while ($my_query->have_posts()) {
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
        echo '</ul>';

Then click this -> Update File or Save. Now your related posts will appear in each of your post. Hope this post useful to you, and see you in the next post.

Thank me ❤️ by sharing this article..
Leave A Reply

[name=Your Name Here] [img=Your Image Url Here] [description=Your Description Here] (facebook=Facebook Profile Url) (twitter=Twitter Profile Url) (instagram=Instagram Profile Url) (bloglovin=Blogvin Profile Url) (pinterest=Pinterest Profile Url) (tumblr=Tumblr Profile Url)
Subscribe Our Newsletter