daily gadgets, computers, and electronic news
18/04
2005

Modify Related Post plugin

Sponsored Links

[php]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[/php]

Pages: 1 2

Modify Related Post plugin is written by cosa and posted under Programming, WordPress , . If you like it, you might consider subscribing to our feed or receive our latest posts via email. Or else, you could also bookmark it to your favourite social bookmark sites. Further information about this article can be found .

No Comments (leave yours)

No comments yet.

Leave a comment