How to Create a Wordpress Related Post Plugin Like A Boss?

This guest post is written by Malinda Alahakoon of Notesofgenius.com


Previously I gave you a detailed step by step guide to prepare your environment to customize Wordpress (self-hosted) like a BOSS. Here we will be customizing the theme you have installed to give it unique features using PHP, HTML and CSS coding.

Wordpress Related Post Plugin

You might ask me �Why should I deal with geeky coding when I can simply install a plug-in?� Yeah, you have a valid point. But once you create your own plug-in you are the God and you make the rules. You can do whatever you wish for. Any sort of customization is just few lines of code away. Most importantly you can give wings to your imagination and develop unique features.

Today you will learn how to customize Wordpress theme step by step and add a new functionality. If you didn�t read �Getting Started with Advanced Wordpress Customization�, it�s time to go back and take a look. If you got some time to play with PHP and CSS, it would be a great support.

I�ll start developing related post plug-in. It�s very important to have one to reduce your bounce rate and deliver a great user experience.

Tip: 4 Ultimate Techniques to Get Traffic With Twitter

Steps to Develop Related Post Plugin


First open Dreamweaver and connect to remote site. There you have to open the file wp-content/theme/yourtheme/function.php. This could be custom_functions.php in some themes. There you will be able to see lots of theme specific operations.
Here I�m going to tell you how to create a related post plug-in. I�m going to find related posts using matching tags.

 Develop Related Post Plugin

  • Scroll down to page and create a new PHP function. Give it an appropriate name.
  • Here is the PHP code to retrieve related posts based on tags. Read the comments to figure out what�s happening in each line.
 
    Tip: 10 Features of a Unique and Strong Brand


    <?php
    function related_posts(){
    global $post;
    $current_post = new wp_query('p='.$post->ID); // retrieve current post
    $tags = wp_get_post_tags($post->ID); // retrieve tags in current post
    if ($tags) { // check whether current post have any tags. If not, no point proceeding further
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; // put tags in current posts to php array
    $args=array( // argument list to retreive related posts
    'tag__in' => $tag_ids, // post with this tags
    'post__not_in' => array($post->ID), // omit current post
    'showposts'=>5, // Number of related posts that will be shown.
    'caller_get_posts'=>1
    );
    $my_query = new wp_query($args); // query db to get related posts and store them in my_query
    if( $my_query->have_posts() ) { // check whether there are any related posts
    echo '<h2>Read Related Posts</h2><div class="related_posts"><ul>'; // title
    while ($my_query->have_posts()) { // display related posts one by one
    $my_query->the_post();
    ?>
    <div class="related_post_item">
    <a href="<?php the_permalink() ?>" rel=bookmark title="<?php the_title_attribute(); ?>"><li><?php the_title(); ?></li></a>
    </div>
    <?php
    }
    echo '</ul></div>';
    }
    }
    $current_post->the_post(); // Reset current post or your comment thread and other functions comes after this will get messed up
    }
    ?>


    • Now your function is ready to display related posts as a simple unordered list.
    • Next part is to include this in your posts page. For that open single.php.
    • Include below code wherever you want to display related posts.


    <?php related_posts();?>

    • Now upload all the files to server and you are DONE!
    • Go to any posts page and you will be able to see related posts items as a list.

    Now you might start thinking how to take this to next level. For that you will need some CSS knowledge and it will be highly dependent on other theme functions. In my blog I have created below related posts items by adding post thumbnail.

    Tip: Top 10 Companies That People Die To Work In!

    wordpress related post widget


    Unfortunately I have no way to give you any more tips since they will be based on your theme. If you have a creative idea and you need help to implement it, never hesitate to contact me @MalindaOnline. I�ll assist you to create what you exactly need by studying your surrounding theme elements.

    You may like to read:



    Malinda Alahakoon

    Malinda Alahakoon is a regular BloggingeHow reader and follower who loves to write on his blog NotesOfGenius. His passion for Blogging is what makes him stand out. You can connect with him @MalindaOnline

    No comments