【发布时间】:2016-08-24 16:52:51
【问题描述】:
我在 Wordpress 中的 taxonomy.php 模板中的以下代码遇到了一些真正的问题。该查询正在运行(即仅从该自定义分类中提取帖子),但它仅显示 2 个帖子(4 个在分类中)。
我使用 $args 将其转换为标准循环的所有努力只会导致来自所有分类法的帖子被拉入页面。我希望它就像添加 posts_per_page => -1 一样简单,但这只会导致整个网站中的每个帖子都显示出来。
据我从法典中了解到,分类页面默认情况下应该拉取相关帖子,而不需要循环?
非常感谢任何帮助!
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(); ?>
【问题讨论】:
-
您能否提供您的自定义分类名称,以便我可以为您提供代码以显示与该分类相关的适当帖子?
-
嗨@laraib,我实际上有6个分类法,每个分类法都有多个“术语”。当用户通过单击“术语”从索引页面单击时使用分类页面 - 然后应该在该“术语”内显示 x 个帖子表单。有道理?我的 6 个分类法是:“主题”、“地点”、“日期”、“受访者”、“时期”和“a-z”。谢谢