有过SEO经验的人都知道在每个分类或者文章周边添加相关文章可以对SEO优化起到很好作用,而且不仅仅单从用户体验上相关文章的作用还可以为我们的用户推荐更多相匹配的文章来留住用户,那么如果您在使用wordpress建站wordpress调用相关文章该如何调用?今天瑞课小编从SEO角度带您了解wordpress调用相关文章,让您的wordpress站点更排名靠前吧。。
其实如果说起wordpress调用相关文章有很多的调用方式,您可以根据标签、作者、分类、sql等获取调用wordpress调用相关文章,下面呢我们将从这几个开始讲解如何wordpress调用相关文章。
wordpress调用相关文章第一种调用方式:按标签
<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
}
}
else {
echo '<li>* 暂无相关文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暂无相关文章</li>';
}
?>
</ul>
wordpress调用相关文章第二种调用方式:按分类
<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
}
}
else {
echo '<li>* 暂无相关文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暂无相关文章</li>';
}
?>
</ul>
wordpress调用相关文章第三种调用方式:按作者
<ul id="author_related">
<?php
global $post;
$post_author = get_the_author_meta( 'user_login' );
$args = array(
'author_name' => $post_author,
'post__not_in' => array($post->ID),
'showposts' => 6, // 显示相关文章数量
'orderby' => date, // 按时间排序
'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
}
}
else {
echo '<li>* 暂无相关文章</li>';
}
wp_reset_query();
?>
</ul>
以上wordpress调用相关文章调用方法就介绍这些其它的就不举例了,而每种wordpress调用相关文章加载速度也有所不同,自己选择wordpress调用相关文章使用就行了。

