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() ?>:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    <?php
    $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) {
    unset($posts[$count]);
    }else{
    $count ++;
    }
    }
    ?>
 
    <?php if ( $posts ) : ?>
    <div class="related_articles">
    <h2>Related Posts</h2>
    <ul>
    <?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 ?>
    </ul>
    </div>
    <?php endif // $posts ?>
    <?php
    $post = $this_post;
    unset($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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
$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),
        'showposts'=>5, // Number of related posts that will be shown.
        'caller_get_posts'=>1
    );
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ) {
        echo '<h3>Related Posts</h3><ul>';
        while ($my_query->have_posts()) {
            $my_query->the_post();
        ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
        <?php
        }
        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.

Related posts:

  1. Show Most Popular Post In Wordpress Without Plugin
  2. Tutorial To Split Wordpress Post Into Separated Pages
  3. How To Shorten Wordpress Blog Post URL For Twitter
  4. Tutorial To Show Ads only on Wordpress Homepage
  5. If DB Cache Does Not Work With WP 2.8 Use DB Cache Reloaded Plugin!