How to Modifying Wordpress Related Post Plugins

First, if you do not have Related Post plugins, you can download it from the author’s website (Link). This little modification will make your Related Post plugin more powerful and useful.

Follow these steps to update the plugin:

  1. Copy these code:
  2. <? if (! function_exists(”related_posts”))
    {

    function related_posts($limit=5, $len=10, $before_title = ”, $after_title = ”, $before_post = ”, $after_post = ”, $show_pass_post = false, $show_excerpt = false, $before_last_title = ”, $after_last_title = ”) {

    global $wpdb, $post, $tableposts;

    if ($before_last_title == ”) {
    $before_last_title = $before_title;
    }
    if ($after_last_title = ”) {
    $after_last_title = $after_title;
    }

    $postcustom = get_post_custom_values(’keyword’);
    if (!empty($postcustom)) {
    $values = array_map(’trim’, $postcustom);
    $terms = implode($values, ‘ ‘);
    } else {
    $terms = str_replace(’-', ‘ ‘, $post->post_name);
    }

    $time_difference = get_settings(’gmt_offset’);
    $now = gmdate(”Y-m-d H:i:s”,(time()+($time_difference*3600)));

    $sql = “SELECT ID, post_title, post_content,”
    . “MATCH (post_name, post_content) ”
    . “AGAINST (’$terms’) AS score ”
    . “FROM $tableposts WHERE ”
    . “MATCH (post_name, post_content) ”
    . “AGAINST (’$terms’) ”
    . “AND post_date < = '$now' "
    . "AND (post_status = 'publish' && ID != '$post->ID’) “;
    if (!$show_pass_post) { $sql .= “AND post_password =” “; }
    $sql .= “ORDER BY score DESC LIMIT $limit”;
    $results = $wpdb->get_results($sql);
    $output = ”;
    if ($results) {
    $index = 1;
    foreach ($results as $result) {
    $title = stripslashes(apply_filters(’the_title’, $result->post_title));
    $permalink = get_permalink($result->ID);
    $post_content = strip_tags($result->post_content);

    $post_content = stripslashes($post_content);
    if (($limit>1) && ($index==$limit)) {
    $output .= $before_last_title .’‘ . $title . ‘‘ . $after_last_title;
    } else {
    $output .= $before_title .’‘ . $title . ‘‘ . $after_title;
    }
    if ($show_excerpt) {
    $words=split(” “,$post_content);
    $post_strip = join(” “, array_slice($words,0,$len));
    $output .= $before_post . $post_strip . $after_post;
    }
    $index++;
    }
    echo $output;
    } else {
    echo $before_title.’No related posts’.$after_title;
    }
    }

    // End of related_posts
    ?>

  3. Go to line 52. It should read:
    if (! function_exists("related_posts"))
  4. Scroll down a little bit and find line contains:
    // End of related_posts
    It should be on around line 110.
  5. Now remove line 52 until above line, and replace it with the new code you have copied from the point number one. Save your works.
  6. The new related_post function will now have 2 extra parameter, $before_last_title and $after_last_title. Modify your theme to match the new function.
    Mine is:
    Related Post: < ?php related_posts(5, 10, '', ', ', '', '', false, false,' and ','.'); ?>

Good luck!

Popularity: 100% [?]

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>