<ul> <?php $post_tags=wp_get_post_tags($post->ID); //如果存在tag标签,列出tag相关文章 $pos=1; $rpls=3; $renum=stripslashes(get_option('related_post_num')); //文章数量 if($post_tags) { foreach($post_tags as $tag) $tag_list[] .= $tag->term_id; $args = array( 'tag__in' => $tag_list, 'category__not_in' => array(NULL), // 不包括的分类ID,可以把NULL换成分类ID 'post__not_in' => array($post->ID), 'showposts' => 0, // 显示相关文章数量 'caller_get_posts' => 1, 'orderby' => 'rand' ); query_posts($args); if(have_posts()): $i==1; while (have_posts()&&$pos<=$renum) : the_post(); update_post_caches($posts); ?> <li><?php include('m_post.php');?></li> <?php $i++; $pos++;endwhile;wp_reset_query();endif; ?> <?php } //end of rand by tags ?> <?php if($pos<=$renum): //如果tag相关文章少于10篇,那么继续以分类作为相关因素列出相关文章 $cats = wp_get_post_categories($post->ID); if($cats){ $cat = get_category( $cats[0] ); $first_cat = $cat->cat_ID; $args = array( 'category__in' => array($first_cat), 'post__not_in' => array($post->ID), 'showposts' => 0, 'caller_get_posts' => 1, 'orderby' => 'rand' ); query_posts($args); if(have_posts()): $i==1; while (have_posts()&&$pos<=$renum) : the_post(); update_post_caches($posts); ?> <li><?php include('m_post.php');?></li> <?php $i++; $pos++;endwhile;wp_reset_query();endif; ?> <?php } endif; //end of rand by category ?> <?php if($pos<=$renum){ //如果上面两种相关都还不够10篇文章,再随机挑几篇凑成10篇 ?> <?php query_posts('showposts='.$renum.'&orderby=rand'); $i==1; while(have_posts()&&$pos<=$renum):the_post(); ?> <li><?php include('m_post.php');?></li> <?php $i++; $pos++;endwhile;wp_reset_query();?> <?php } ?> </ul>
代码中的 m_post.php 就是文章输出格式文件,可自定义