willin版相关文章代码写成函数 函数修改zww.me
文章转载:http://zww.me
把下面的代码扔进主题的 functions.php 里面:
/* Related Posts 函数:出自kan.willin.org,函数修改zww.me */
function RelatedPosts($post_num = 5) {
if(is_single()) {//只在文章页使用
$related_posts = 'Related Posts:
- ';
//$exclude_id = $post->ID; // 單獨使用要開此行
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) { $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->name . ',';
$args = array(
'post_status' => 'publish',
'tag_slug__in' => explode(',', $tags), // 只選 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();
$related_posts .= '
- '.get_the_title().' '; $exclude_id .= ',' . $post->ID; $i ++; } wp_reset_query(); } if ( $i < $post_num ) { // 當 tags 文章數量不足, 再取 category 補足. $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ','; $args = array( 'category__in' => explode(',', $cats), // 只選 category 的文章. 'post__not_in' => explode(',', $exclude_id), 'caller_get_posts' => 1, 'orderby' => 'comment_date', 'posts_per_page' => $post_num - $i ); query_posts($args); while( have_posts() ) { the_post(); $related_posts .= '
- '.get_the_title().' '; } wp_reset_query(); } $related_posts .= '
(里面的5就是显示相关的文章数量)
文章目录
- 上一篇: 骁男
- 下一篇: WordPress mb_strimwidth() 函数 截断补充