回复至:求!文章内页底部相关文章利用标签调用相关内容的代码

孙锡源
  • 文章数量: 704
@ibadboy

以下代码会显示与当前文章具有相同标签的四篇文章,你可以把他加入到 single.php 模板中你想显示的位置上。

但是这段代码并不包含文章的展示样式,也就是说你需求的样式需要你自己写,因为每个主题都不一样,所以没办法提供通用的代码。不过这也不难,可以去文章列表页面复制已有的代码。

<?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 ),
        'posts_per_page'   => 4,
        'caller_get_posts' => 1
    );
    $my_query  = new WP_Query( $args );
    if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
          <a href="<?php the_permalink() ?>" rel="bookmark"
             title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

        <?php
        endwhile;
    }
    wp_reset_query();
}
?>
来自, 山东, 中国