Quote:
Originally Posted by Rick Diculous
I sometimes come accros blogs using a related post plugin that shows thumbs instead of a text link. Anyone know a good plugin that can do this?
|
or you could go without a plugin, maybe something along these lines?
Code:
<?php
function my_related_posts_thumbnail($width = 120,$height = 160){
global $post;
$permalink = get_permalink($post->ID);
$title = $post->post_title;
$post_img = '';
ob_start();
ob_end_clean();
$output = preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$post_img_src = $matches [1];
if(empty($post_img_src)){
$post_img = '<a href="' . $permalink . '" title="'.$post->post_title.'"><img src="'.get_bloginfo("template_url").'/images/default_thumb.jpg" alt="'.$post->post_title.'" />'.$post->post_title.'</a>';
}else{
$post_img = '<a href="' . $permalink . '" title="'.$post->post_title.'"><img src="'.$post_img_src.'" alt="'.$post->post_title.'" />'.$post->post_title.'</a>';
}
echo $post_img;
}
function my_related_posts($post_num = 4) {
global $post;
$exclude_id = $post->ID;
$i = 0;
$cats = '';
foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
$args = array(
'category__in' => explode(',', $cats),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'rand',
'posts_per_page' => $post_num - $i
);
query_posts($args);
while( have_posts() ) { the_post();
echo "<li>" . my_related_posts_thumbnail() . "</li>";
$i++;
}
wp_reset_query();
if ( $i == 0 ) echo '<li>No related posts!</li>';
}
?>