【问题标题】:How to adapt this wordpress loop query code to include pagination?如何调整这个 wordpress 循环查询代码以包含分页?
【发布时间】:2013-02-23 08:02:56
【问题描述】:

我已经获取了以下代码来查询我在 Wordpress 中的帖子,并且所有工作都按预期工作。

我现在想为此添加分页。既然我知道它可以工作,我不想过多地更改代码,任何人都可以建议最好的方法来调整它以包含分页吗?

我希望它最多显示18 posts per page 并在其他页面上显示nextprev links(如果存在)。此代码用于自定义类别模板,但我还在阅读设置下设置了一个静态页面设置,并且主要帖子的显示使用 home.php,我想使用相同或相似的循环代码来分页.任何帮助表示赞赏。代码如下:

<div id="Items">
<ul>
<?php
// Grid sorted alphabetically
if (is_category('categoryName')) 
{
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC', 'category' => 41 );
$categoryNameposts = get_posts( $args ); 
}
foreach( $categoryNameposts as $post ) :    setup_postdata($post); 
?>
<li><?php get_template_part( 'content', get_post_format() ); ?></li>
<?php endforeach; ?>
</ul>
</div><!-- #ItemsEnd -->

【问题讨论】:

    标签: foreach pagination wordpress


    【解决方案1】:

    您应该让 wordpress 知道您希望在 query_posts 中每页显示多少帖子。用这两行替换你的 $args:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array( 'posts_per_page' => 18, 'paged' => $paged, 'orderby'=> 'title', 'order' => 'ASC', 'category' => 41 );
    

    现在显示 Next-Previous 链接:

    <div class="paginationClass">
       <?php next_posts_link(); ?>
       <?php previous_posts_link(); ?>
    </div>
    

    同样在 is_category 函数中,使用 categorySlug,而不是 categoryName。

    【讨论】:

    • 谢谢苏菲,这限制了帖子的数量准确但没有出现下一个和上一个链接?如果我在它们的结束标签后面键入一些标准文本,那么它会显示在页面上,所以我知道它应该放在哪里,但那里什么也没有?有什么想法吗?
    • @M.F 您是否将下一个上一个链接部分放入循环中?你需要把它放在 endwhile 之后和 endif 之前。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2013-04-19
    • 2020-01-14
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    相关资源
    最近更新 更多