wordpress中有标签页(tags)的设置,但是却没能实现相关tag文章的推荐功能
通过插件也可实现,但我们还是本着能省则省的原则,分享代码给大家。
<h3>猜你喜欢 </h3> <?php $post_tags=wp_get_post_tags($post->ID); //如果存在tag标签,列出tag相关文章 $pos=1; 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 'post__not_in' => array($post->ID), 'showposts' => 0, // 显示相关文章数量 'caller_get_posts' => 1, 'orderby' => 'rand' ); query_posts($args); if(have_posts()):while (have_posts()&&$pos<=5) : the_post(); update_post_caches($posts); ?> <li><span><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php if(get_the_title())the_title();else the_time('Y-m-d'); ?></a></span> <span><?php the_time('Y-m-d') ?> <?php the_category('/','') ?></span></li> <?php $pos++;endwhile;wp_reset_query();endif; ?> <?php } //end of rand by tags ?> <?php if($pos<=5): //如果tag相关文章少于5篇,那么继续以分类作为相关因素列出相关文章 $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()): while (have_posts()&&$pos<=5) : the_post(); update_post_caches($posts); ?> <li><span><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute();?>"><?php if(get_the_title())the_title();else the_time('Y-m-d'); ?></a></span> <span><?php the_time('Y-m-d');if($options['tags']):the_tags('', '/', '');endif; ?></span></li> <?php $pos++;endwhile;wp_reset_query();endif; ?> <?php } endif; //end of rand by category ?> <?php if($pos<=5){ //如果上面两种相关都还不够5篇文章,再随机挑几篇凑成5篇 ?> <?php query_posts('showposts=5&orderby=rand');while(have_posts()&&$pos<=5):the_post(); ?> <li><span><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if(get_the_title())the_title();else the_time('Y-m-d') ?></a></span> <span>[ <?php the_category('/',''); ?>]</span></li> <?php $pos++;endwhile;wp_reset_query();?> <?php } ?>
以下代码插入到外观》编辑》single.php 文章页面中
具体位置,你先用审查元素考察一下再插入。
<?php if(!themify_check('setting-comments_posts')): ?>
一般都放在这上面(评论框代码)之前就可以了。