您通过以下方式限制您的查询posts_per_page 要获得五个结果,请使用-1. 不使用$wp_query 作为变量名,因为它与global 具有相同名称的变量,这可能导致干扰。但也就是说,您完全不需要在tag.php。您已经在查询特定标记,使用?tag=abc 或/tag/abc 在您的URL中。WordPress捕捉到这一点并选择正确的模板,并将查询限制到该标记。有一个层次结构tag template (codex page; read it for more information) 选择文件:
标记slug。php
tag-id.php标记。php存档。php索引。php如果可用,则顺序是从第一到第五,如果没有,WordPress正在寻找下一个。此外,我建议
Theme Developer Handbook 一本好书,尤其是关于
Template Files. 最后,您只需要一个模板文件来满足您的需要。下面是示例性模板文件。
<?php 
/**
 * The template for displaying tag pages.
 *
 * @package WordPress
 * @subpackage Theme
 */ 
?>
<?php get_header(); ?>
<!-- tag | start -->
<div class="row">
  <div class="col-xs-12">
    <?php the_archive_title( \'<div class="page-header"><h1 class="page-title">\', \'</h1></div>\' ); ?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12">
    <?php if ( have_posts() ) : ?>
    <?php $description = get_the_archive_description(); ?>
    <?php if ( ! empty( $description ) ) :?>
    <div class="archive-description">
      <?php echo $description; ?>
    </div>
    <?php endif; ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <!-- tag loop | start -->
    <div id="content" <?php post_class(); ?>>
      <div class="row">
        <div class="col-xs-12">
          <?php the_title( \'<div class="post-header"><h2 class="post-title">\', \'</h2></div>\' ); ?>
          <?php if ( has_post_thumbnail() ) : ?>
          <div class="entry-thumbnail">
            <?php the_post_thumbnail(); ?>
          </div><!-- .entry-thumbnail -->
          <?php endif; ?>
          <div class="entry-content">
            <?php the_excerpt(); ?>
          </div><!-- .entry-content -->
        </div>
      </div>
    </div><!-- #content -->
    <!-- tag loop | end -->
    <?php endwhile; ?>
    <?php endif; ?>
  </div>
</div>
<!-- tag  | end -->
<?php get_footer(); ?>