2017.8.17 wordpress 获取相关内容

标签相关
<div class="tagmor">
<h3>相关文章</h3>
<ul>
<?php $tags = wp_get_post_tags( $post->ID );
if ( $tags ) {
$first_tag = $tags[ 0 ]->term_id;
$args = array(
'tag__in' => array( $first_tag ),
'post__not_in' => array( $post->ID ),
'showposts' => 10,
'caller_get_posts' => 1
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ): $my_query->the_post();
?>
<li>* <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title();?></a></li>
<?php endwhile;
}
else {
echo '<li>* 暂无相关文章</li>';
}
}
wp_reset_query();
?>
</ul>
</div>
作者相关
获取该文章作者的其他文章来充当相关文章。
<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>
分类相关
通过获取该文章的分类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
}
}
else {
echo '<li>* 暂无相关文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暂无相关文章</li>';
}
?>
</ul>
显示友情链接
<ul><?php get_links_list(); ?></ul>
相关文章