[WordPress教程]WordPress根据文章相关标签代码实现相关文章

我们都知道,WordPress下每个标签都记录了相关文章列表。当某个用户拜访某篇文章时,你能够猜想这篇文章标签下的其它文章也是用户感兴趣的。 而且,我们还能够进一步完善这个功用,比如在发布文章时,文章标签设定要尽量符合文章的中心思想,而且要多维度的发掘文章标签,因为一篇文章往往不止一个标签。当分类精准和文章添加后,命中率也会有所提高。 下面介绍代码完结办法: 办法一 首要获取文章的悉数标签,接着获取这些标签下的 n 篇文章,那么这 n 篇文章或许用户也会感兴趣。

<ul id="tags_related">
<?php
global $post;
$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>

方法二:分类相关
本方法是通过获取该文章的分类id,然后获取该分类下的文章,来达到获取相关文章的目的。

<ul id="cat_related">
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$args = array(
'category__in' => array( $cats[0] ),
'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>


办法三:标签相关,SQL获取
获取相关文章的原理与办法一相似,不过在获取文章的时分是以SQL句子来直接读取数据库,然后随机获取6篇相关文章记载,而不是WordPress的函数query_posts().

<ul id="tags_related">
<?php
global $post, $wpdb;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
$tag_list = '';
foreach ($post_tags as $tag)
{
// 获取标签列表
$tag_list .= $tag->term_id.',';
}
$tag_list = substr($tag_list, 0, strlen($tag_list)-1);

$related_posts = $wpdb->get_results("
SELECT DISTINCT ID, post_title
FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
AND ID = object_id
AND taxonomy = 'post_tag'
AND post_status = 'publish'
AND post_type = 'post'
AND term_id IN (" . $tag_list . ")
AND ID != '" . $post->ID . "'
ORDER BY RAND()
LIMIT 6");
// 以上代码中的 6 为限制只获取6篇相关文章
// 通过修改数字 6,可修改你想要的文章数量

if ( $related_posts ) {
foreach ($related_posts as $related_post) {
?>
<li><a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li>
<?php  } } else { ?>
<li>暂无相关文章</li>
<?php } } ?>
</ul>

办法四:分类相关,SQL获取
获取相关文章的原理与办法二相似,不过在获取文章的时候是以SQL句子来直接读取数据库,然后随机获取6篇相关文章记录,而不是WordPress的函数query_posts().

<ul id="cat_related">
<?php
global $post, $wpdb;
$cats = wp_get_post_categories($post->ID);
if ($cats) {

$related = $wpdb->get_results("
SELECT post_title, ID
FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id
AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'
AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
AND {$wpdb->prefix}posts.post_status = 'publish'
AND {$wpdb->prefix}posts.post_type = 'post'
AND {$wpdb->prefix}term_taxonomy.term_id = '" . $cats[0] . "'
AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'
ORDER BY RAND( )
LIMIT 6");

if ( $related ) {
foreach ($related as $related_post) {
?>
<li>* <a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li>
<?php  } } else { ?>
<li>* 暂无相关文章</li>
<?php } }?>
</ul>

 

声明: 1、本站所有文章仅供参考,如有侵权 请联系我们删除 meng#yimiaonet.com #换成@ 2、文章大部分源自网络或ai生成,文章不作为任何依据,仅供参考。 3、本站的所有源码都是在网络上转载或由用户投稿,仅供参考学习使用,请您务必在下载后24小时内删除。 4、本站下载的所有源码等内容不得用于任何违反相关法律法规的用途,一经发现 我们立即向有关部门报备。 5、### 本站除商业栏目外 其他资源均来自于网络或用户投稿,如有侵权 请及时联系我们删除,感谢您的支持与理解,让我们一起支持创作者权益。 6、如果您需要商用,可以联系客服定制开发或购买商业源码栏目内的内容,当然也可以联系部分源码的原作者;我们最终一切版权。 7、您注册本站会员后,如果需要注销账号等适宜,请联系客服。