WordPress代码实现调用相关文章的5种方法(一)
这里北京seo大勇整理编写了几种用代码实现相关文章的方法,这其中会详细标明各部分代码的作用,以及如何自定义你想要的功能,希望对大家有所帮助,有什么问题可以给本文发表评论,我会及时给你回复!
方法一:标签相关
首先获取文章的所有标签,接着获取这些标签下的 n 篇文章,那么这 n 篇文章就是与该文章相关的文章了。现在可以见到的WordPress相关文章插件都是使用的这个方法。下面是实现的代码:
<?php
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {foreach ($post_tags as $tag)
{
// 获取标签列表
$tag_list[] .= $tag->term_id;
}// 随机获取标签列表中的一个标签
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
// 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表
$args = array(
‘tag__in’ => array($post_tag),
‘category__not_in’ => array(NULL), // 不包括的分类ID
‘post__not_in’ => array($post->ID),
‘showposts’ => 6, // 显示相关文章数量
‘caller_get_posts’ => 1
);
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post(); update_post_caches($posts); ?>
<li>* <a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>
<?php endwhile; else : ?>
<li>* 暂无相关文章</li>
<?php endif; wp_reset_query(); } ?>
</ul>
作者:北京婚纱摄影 @北京婚纱摄影网络推广
原文地址:http://www.baby521.net/seo-baidu/202.html
版权所有,转载时必须以链接形式注明作者和原始出处及本声明。
你可能还喜欢一下文章:
看了很有帮助