我在Wordpress的taxonomy.php
模板中遇到以下代码时遇到了一些问题。该查询正在运行(即仅从该自定义分类中提取帖子),但它只显示2个帖子(4个在分类中)。
我使用$ args将其转换为标准循环的所有努力只会导致所有分类法中的帖子被拉入页面。我希望它像添加posts_per_page => -1
一样简单,但这只会导致整个网站中的每个帖子都显示出来。
正如我从codex中理解的那样,分类法页面应默认拉出相关帖子,而不需要循环?
非常感谢任何帮助!
taxonomy.php
<?php get_header(); ?>
<main>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
更新
<main>
<?php
$args = array(
'posts_per_page' => -1
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
答案 0 :(得分:2)
如果您有6种不同的分类法,那么将有6种不同的模板文件来适当地显示这些分类法。在您的情况下,您的模板将为taxonomy-topics.php
taxonomy-places.php
taxonomy-dates.php
taxonomy-interviewee.php
taxonomy-period.php
和taxonomy-a-z.php
通过这种方式创建这些模板后,您的模板将显示相应的帖子。要实现这一点,您可以使用posts_per_page
参数,或者您可以访问此页面以更好地了解有关提取帖子WP_Query Codex Page希望现在有意义