本文初步讲解wordpress相关文章列表的实现,并进行条数控制的探讨。本文只是一种思路的引导,读者朋友可根据自己的需求,按照这种思路进一步开发,如果你有新的思路,也请和我们分享。
一、核心代码 ↑
以往,我们通过各种手段以获得相关的列表,但现在,我们其实更多的利用query_posts函数来实现这一切。下面是更新后的程序代码。
<?php $post_id = get_the_ID(); $cat_arr = array(); $cats = get_the_category(); foreach($cats as $cat){ $cat_arr[] = $cat->cat_ID; } $tag_arr = array(); $tags = get_the_tags(); if($tags)foreach($tags as $tag){ $tag_arr[] = $tag->term_id; } if(empty($tag_arr)){ $tag_arr[0] = '不可能有的标签_'.COOKIEHASH; } query_posts(array( 'posts_per_page' => 6, 'ignore_sticky_posts' => 1, 'caller_get_posts' => 1, 'orderby' => 'date', // 在下文中我建议使用rand,但其实随机列表并不利于seo 'tag__in' => $tag_arr, 'post__not_in' => array($post_id) // 排除本文本身 )); ?> <?php if(have_posts()) : // 如果通过标签找到了相关文章 ?> <div id="related_posts"> <h3>相关文章</h3> <ul> <?php while(have_posts()):the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo mb_strimwidth(get_the_title(),0,42,'...'); ?></a></li> <?php endwhile; ?> <?php global $wp_query; if($wp_query->post_count < 6) : // 如果通过标签找到的文章不到6篇,你可以改为自己想要的篇数,一般可能会改为10 query_posts(array( 'posts_per_page' => (6 - $wp_query->post_count), // 这里就要找到6篇为总数的剩下篇数 'ignore_sticky_posts' => 1, 'caller_get_posts' => 1, 'orderby' => 'date', 'tag__not_in' => $tag_arr, // 因为前面已经找到了标签相关的文章,所以这里要排除 'category__in' => $cat_arr, // 用相同的分类找相关文章 'post__not_in' => array($post_id) // 和前面一样,排除文章本身 )); ?> <?php while(have_posts()):the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo mb_strimwidth(get_the_title(),0,42,'...'); ?></a></li> <?php endwhile; ?> ??? ??? ??? ??? <?php endif; ?> </ul> </div> <?php else : // 第一次通过标签没有找到相关文章的话,就要往下执行 ?> <?php query_posts(array( 'posts_per_page' => 6, 'ignore_sticky_posts' => 1, 'caller_get_posts' => 1, 'orderby' => 'date', 'category__in' => $cat_arr, // 如果没有通过标签找到相关文章,就直接使用分类来找相关文章 'post__not_in' => array($post_id) )); ?> <?php if(have_posts()): ?> <div id="related_posts"> <h3>相关文章</h3> <ul> <?php while(have_posts()):the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo mb_strimwidth(get_the_title(),0,42,'...'); ?></a></li> <?php endwhile; ?> </ul> </div> <?php endif; ?> <?php endif; ?> <?php wp_reset_query(); ?>
但实际上,这种方法仍然显得繁复。我一直在考虑,有没有一种方法,现将所有相关文章的ID找到之后再进行query_posts呢?我想应该可以的,只不过需要多一些数据判断和查询吧。但今天暂且到这里,以后再来写。
下方是以前写过的代码,可以帮助你理解。
<?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<=10) : the_post(); update_post_caches($posts); ?>
<li><span class="title"><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 class="meta"><?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<=10): //如果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()): while (have_posts()&&$pos<=10) : the_post(); update_post_caches($posts); ?>
<li><span class="title"><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 class="meta"><?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<=10){ //如果上面两种相关都还不够10篇文章,再随机挑几篇凑成10篇 ?>
<?php query_posts('showposts=10&orderby=rand');while(have_posts()&&$pos<=10):the_post(); ?>
<li><span class="title"><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 class="meta">[ <?php the_category('/',''); ?>]</span></li>
<?php $pos++;endwhile;wp_reset_query();?>
<?php } ?>
上文中,红色部分为关键部分,蓝色部分为呈现部分
二、思路讲解 ↑
1、相关性:标签(tag)相关+分类(category)相关。
我们采用这种相关方法,因为无法做到向搜索引擎一样检索全文确定相关性,我们只能通过标签、分类来进行判断。而在一般情况下,博客文章相关性也是靠这两者,有些相关文章虽然靠这两者不能被放出,但我们放出的肯定都是相关文章。
2、标签相关文章详解:
我们先用$post_tags=wp_get_post_tags($post->ID);获取文章的标签列表(数组),再通过foreach($post_tags as $tag) $tag_list[] .= $tag->term_id;获取这些标签的ID,并将这些ID组成数组$tag_list。使用函数query_posts();查询这些标签ID下的文章,通过while()显示出来。
3、分类相关文章详解
我们通过获取当前文章所在的分类,将这些分类中的文章挑选一些出来呈现给读者。其原理和上文标签相关文章一样。
4、文章数量的控制
为了让不同的页面在格式上很整齐,我们规定这些相关文章是随机排列的,不是按某项属性顺序或倒序排列,我们使用了'orderby' => 'rand'。
同时为了让文章列表显得完整,我们希望每篇文章后的这些相关文章列表都输出10条。我们的思路是:用一个计数参数,其初始值为0,每显示一篇文章就增加1($pos++)。当$pos>=10时就不再输出文章。
如果根据标签而列出的文章数已经超过十篇,那么不再执行下面的分类和随机相关,如果不到10篇,则继续执行;当执行完分类相关之后仍然还不到10篇,我们调用一些随机文章,直到满10篇为止。
注意代码中的orderby=rand,一般而言,相关文章都是使用rand随机排序的,你可以尝试在调用时按照date时间排序
想问一下如何使相关文章能够静态下来?每次刷新文章页面的话,相关文章也会随机动态调用文章
用一个数组参数记录下已经列出来的文章ID,在后面的query_posts中排除这些文章
这个会相同的文章调多次..
难道只有我一个人是这样么..
比如这个文章 在TAG调用出来了一次,,然后是随机还是相同目录下又会出现一次…
就是有重复….哥们你能添加个函数去掉重复么
这个超好用阿!!!!
果断收了,
哈哈,网上有很多这样的教程,不过还是这篇比较有个性啊。祝博主新年快乐哦。常来我的小站逛逛哦 嘻嘻~~