【问题标题】:Wordpress: Limit posts per page in categoryWordpress:限制类别中每页的帖子
【发布时间】:2012-11-19 22:33:20
【问题描述】:

在我的 index.php 中,我使用此代码来限制每页的帖子,它可以正常工作:

$showposts = 5;
$do_not_show_stickies = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category__in' => $cat, 'showposts' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);

$loop2query = new WP_Query($args);

query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); ?>

<div class="blogpost"> ... </div>

<?php endwhile; endif;
posts_nav_link(); // Navigating the pages with $showposts each. ?>

相同的代码在 category.php 中不起作用,所以我将其更改为以下,但仍然不起作用:

$showposts = 4;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

if (have_posts()) { while (have_posts()) { the_post(); ?>
    <div class="blogpost"> ... </div>

<?php } }

else { ?>
    <p>There are no blog posts in this category.</p>
<?php } ?>

<?php posts_nav_link(); // Navigating the pages with $showposts each. ?>

我尝试在 category.php 中使用 if(have_posts()) : while (have_posts()) : the_post(); ?&gt; [...] 更改该行以使其与 index.php 中的该行相似,但我尝试过的没有任何效果。

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    Wordpress 对此有一个设置,可以在 SETTINGS -> READING -> Blog pages show at most

    下的管理区域中找到

    您可以使用它而不是自定义修改您的查询。这可能会使您在以后的项目中更容易维护。

    【讨论】:

    • 谢谢!我什至没有想过这样的解决方案
    【解决方案2】:

    使用 posts_per_page 参数 (Codex here)

    $args = array('category__in' => $cat, 'posts_per_page' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);
    

    【讨论】:

      【解决方案3】:

      在您的第一个示例中,您实际上有两个查询:new WP_query,然后是 query_posts,因此您需要删除其中一个,因为这是多余的。在第二个示例中,恰恰相反,您没有任何查询(尽管 WordPress 可能会默认执行一个查询,具体取决于调用此页面的位置)。所以无论如何,在你的第二个例子中使用$showposts 是没有意义的,因为你没有在之后执行查询...... if (have_posts()) 通常用于处理来自 WordPress 的默认(在您的页面代码中不可见)循环或处理您之前声明的查询(通常使用 query_posts())。 正如@Samuel 所说,要使用的参数是posts_per_page,但我认为你还没有到那里,你应该首先开始学习如何执行查询,所以你可以从阅读query_posts 上的WordPress codex 开始,这将是第一个去的最佳地点:http://codex.wordpress.org/Function_Reference/query_posts.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        • 2011-11-07
        • 2016-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多