1, related by categories
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$args = array(
‘category__in’ => array( $cats[0] ),
‘post__not_in’ => array( $post->ID ),
‘showposts’ => 6,
‘caller_get_posts’ => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a>
<?php
}
}
else {
}
wp_reset_query();
}
else {
}
?>
2, related by tags
<?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
foreach ($post_tags as $tag) {
$tag_list[] .= $tag->term_id;
}
$post_tag = $tag_list[ mt_rand(0, count($tag_list) – 1) ];
$args = array(
‘tag__in’ => array($post_tag),
‘category__not_in’ => array(NULL),
‘post__not_in’ => array($post->ID),
‘showposts’ => 6,
‘caller_get_posts’ => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>“><?php the_title(); ?></a>
<?php
}
}
else {
}
wp_reset_query();
}
else {
}
?>
3, learn from : http://www.ludou.org/how-to-generate-related-posts-in-wordpress.html
Leave a Reply