Related Posts for b2evolution
Quite a few people have asked me how I'm achieving the "might be related to .." entries that are included with some of the posts on this site. First, I am now using WordPress, and there is a WP hack for it. Since a few of the people that have asked are using b2evolution, I decided to see if I could adapt the WP hack to work with b2evolution. With a little help I've managed to do it.
First things first though. In order to do this you must make a change to one table in your database. I've written this assuming you are going to use phpmyadmin to access MySQL.
- ALTER TABLE `evo_posts` ADD FULLTEXT `post_related` (
- `post_title` ,
- `post_content`
- )
Notice in the above that the table that contains your posts is hardcoded as evo_posts. If you are using another prefix you obviously need to change that.
What that does is create a full text index on your post_content and post_title fields.
Now, the function.
The standard disclaimer applies here. This probably is not the prettiest way to accomplish this, however it does work.
function relatedposts($limit=5, $before='<li>', $after='</li>') {
global $DB, $Item, $tableposts ;
$file = get_bloginfo('blogurl');
$terms = str_replace('-', ' ', $Item->title);
$sql = "SELECT ID, post_title, "
. "MATCH (post_title, post_content) "
. "AGAINST ('$terms') AS score "
. "FROM $tableposts WHERE "
. "MATCH (post_title, post_content) "
. "AGAINST ('$terms') "
. "AND (post_status = 'published' && ID != '$Item->ID') ";
$sql .= "ORDER BY score DESC LIMIT $limit";
$results = $DB->get_results($sql);
if ($results) {
foreach ($results as $result) {
$lid = $result->ID;
$post_title = stripslashes($result->post_title);
$title = htmlspecialchars(stripslashes($result->post_title));
$permalink = url_add_param( $file, 'p='. $lid. '&c=1' );
echo $before
.'<a href="'. $permalink .'" rel="bookmark" title="Related Link: ' . $post_title . '">'
.$title.'</a>'.$after;
}
} else {
echo $before.'Not a damn thing. Woops!'.$after;
}
}
?>
By default is will display 5 posts it thinks are related, based on what it indexes in your database. Those 5 posts will be displayed in a list. Calling this function is simple. The default usage would be
If you want to reduce the number of returned links or change the way it is displayed, you can use something like
That example will return 2 related posts that are followed by commas.
On this site, it's used similar to this:
That returns one related post and no formatting.
That's all there is. I should add that obviously this needs to be used within your post loop. And once again, so we're clear, this is a modification of an already existing WordPress hack. This code works with b2evolution.
Oh, and if you are wondering how to add the function, where to put it, etc.. what I recommend is taking advantage of the legacy hacks support that b2evolution left in -- Copy the function into a file, name the file hacks.php and upload it to your conf/ directory.
December 8th, 2004 · code and tech-talk · whoo · Comments (0)


