WordPress:样式分类法 slug 页面

WordPress: Styling taxonomy slug page

我有一个名为 Topics 的自定义 taxonomy

Topics 目前分为三类:

注意上面每个类别的 post 的计数。

当用户转到主题页面时,即 /topics/news,我想整齐地显示与 news 相关的所有 post,因此希望编写自定义标记。

为此,我遇到了 taxonomy templates,但结果很奇怪。

首先,我在 /topics/news。从上图中,可以看到News有2个post。

这是我的 taxonomy-topics.php 文件:

<?php get_header();

  if ( have_posts() ){
    while ( have_posts() ) {
      the_title();
    }
  }

get_footer(); ?>

目前只是想显示 news post 的标题。但是,它正在循环并多次打印一个 post 的标题。似乎发生了无限循环。

您必须调用 the_post() 以便 post 索引移动到主查询中 posts 数组中的下一个(即 $wp_query 全局):

while ( have_posts() ) {
    the_post(); // call this or you'll be stuck in an infinite loop! :)
    the_title();
}