【问题标题】:Show all the posts from a category on a single page in Wordpress在 Wordpress 的单个页面上显示某个类别的所有帖子
【发布时间】:2015-07-17 14:12:43
【问题描述】:

我是 php 和 wordpress 的新手。

我将帖子类别用作一个简单的目录,并希望用户在点击一个类别时查看每个帖子,而不是对结果进行分页。

在主题附带的 Archive.php 中,我尝试过这样的事情:

    /* Get the current category and increase the number of posts shown */
    <?php 
    $category = $wp_query->get_queried_object()->slug;
    query_posts('category_name=$category&showposts=100'); 
    ?>

    <div id="content">
        <?php if ( have_posts() ) : ?>
        <?php /* The loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content', get_post_format() ); ?>
        <?php endwhile; ?>
        <?php else : ?>
            <?php get_template_part( 'content', 'none' ); ?>
        <?php endif; ?>
    </div><!-- /content -->

但是,它不起作用。点击类别显示未找到任何帖子。

我确信有一种更好、更简单的方法可以做到这一点。

第二个问题是,将这种逻辑仅应用于我拥有的类别子集的好方法是什么?我想我可以创建一个类别数组并进行查询,但这似乎很笨拙。

非常感谢。

【问题讨论】:

  • 您无需在存档页面上执行query_posts()...默认查询已包含类别帖子。
  • 好的,谢谢,我想我只是用它来更改查询以显示更多帖子,如果我手动输入类别名称来代替 $category 就会这样做。必须有更好的方法来增加显示的帖子数量(可能显示“全部”)。

标签: wordpress


【解决方案1】:

不确定它是否能解决您的问题,但您的代码中存在错误:

<?php
query_posts('category_name=$category&showposts=100');

单引号中的变量名没有任何作用。变量扩展只发生在双引号字符串中(参见here):

<?php
query_posts("category_name=$category&showposts=100");

更好的是跳过变量扩展并进行适当的连接:

<?php
query_posts('category_name=' . $category . '&showposts=100');

【讨论】:

  • 啊,太好了,解决了,谢谢。我一直在寻找正确的语法,但我不知道 php.ini 中单引号和双引号之间的区别。显然我缺乏咖啡。不过我也想知道有没有办法让 showposts 等同于所有帖子,而不是固定数量?
猜你喜欢
  • 2012-02-17
  • 2017-12-22
  • 2013-10-03
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多