【问题标题】:Wordpress taxonomy.php loop only showing 2 posts?Wordpress taxonomy.php 循环只显示 2 个帖子?
【发布时间】: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”。谢谢

标签: php wordpress


【解决方案1】:

如果您有 6 个不同的分类法,那么将有 6 个不同的模板文件来适当地显示这些分类法。在您的情况下,您的模板将是 taxonomy-topics.php taxonomy-places.php taxonomy-dates.php taxonomy-interviewee.php taxonomy-period.phptaxonomy-a-z.php

这样,一旦创建了这些模板,您的模板就会显示相应的帖子。要实现这一点,您可以使用posts_per_page 参数,或者您可以访问此页面以更好地了解获取帖子WP_Query Codex Page 希望现在有意义

【讨论】:

  • 你好@laraib。感谢您的回答,但是,我无法使其正常工作。我已经创建了 taxonomy-places.php 进行测试,并且我已经使用添加到 posts_per_page 参数中的更新代码更新了问题,但是我再次留下了网站中的每个帖子?
  • 啊,我刚刚意识到只有 2 个帖子显示的原因是因为我在设置>阅读中设置了“博客页面最多显示”设置为 2。这是因为我想要这个适用于网站的另一部分。这可以覆盖吗?
  • 很高兴听到您的问题已解决。是的,你可以,但默认情况下它需要你的主循环......你可以使用 WP_Query 在分类法中获取你的帖子,因此这些不会影响你在其他页面中的设置,并确保你在使用后重置你的循环。
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 2012-06-29
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多