最近想给博客做一个相关文章的推荐,文章是基于标签的推荐,可以自定义推荐篇数。可以根据自己的需要设置篇数,及其呈现方式,该文章最后就是一个相关文章推荐的实例,可以点击看看,我贴出自己的代码直接套用就行了,不一定什么都自己写。
1、首先是在function.php里面追加以下函数代码:
最好先备份一下防止错误,修改wordpress的东西得有这个习惯
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// 相关文章推荐 function clin_related_article(){ ?> <div class="related_div"> <div class="related_title">推荐文章</div> <ul class="related_posts"> <?php $post_num = 8; // 推荐页数 $exclude_id = $post->ID; // 当前文章id $posttags = get_the_tags(); $i = 0; // 获取标签 if ( $posttags ) { $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ','; $args = array( 'post_status' => 'publish', 'tag__in' => explode(',', $tags), 'post__not_in' => explode(',', $exclude_id), 'caller_get_posts' => 1, 'orderby' => 'comment_date', 'posts_per_page' => $post_num, ); query_posts($args); while( have_posts() ) { the_post(); ?> <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php $exclude_id .= ',' . $post->ID; $i ++; } wp_reset_query(); } if ( $i < $post_num ) { $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ','; $args = array( 'category__in' => explode(',', $cats), 'post__not_in' => explode(',', |