【问题标题】:WordPress custom query paginationWordPress 自定义查询分页
【发布时间】:2017-04-29 14:46:10
【问题描述】:

我有一个 WordPress 网站,我在主页上列出了更多类别的内容。

我的问题是,有没有一个插件可以让我对某个类别的结果进行分页?我的意思是$this->plugin_paginate('category_id'); 之类的东西?

最好的问候,

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    如果您使用标准的 Wordpress 循环,即使使用 query_posts 作为类别,使用通常的 posts_nav_link 也会自动进行分页。您是否尝试在同一页面上为多个查询和多个类别分页?

    编辑 11/20:我在一个页面的几个不同位置使用它来显示一个类别中的最新帖子:

    <?php
    $my_query = new WP_Query('category_name=mycategory&showposts=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    ?>
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>
    

    然后该链接会转到为该类别分页的类别页面:Category Templates « WordPress Codex

    我不知道如何在同一页面上对不同类别进行分页。必须是可能的。可以在Wordpress forums问。

    【讨论】:

      【解决方案2】:

      这听起来像是一个简单的、格式良好的 query_posts() 调用就可以做到的事情。我怀疑你甚至需要依赖插件。 :)

      我假设您熟悉 query_posts() 函数,所以让我们继续以这个示例为基础:

      // let's get the first 10 posts from category ID 3
      query_posts('posts_per_page=10&cat=3');
      while(have_posts()):the_post();
          // do Wordpress magic right here
      endwhile;
      

      现在,要从类别 3 中获取第 11 到第 20 个帖子(即 NEXT 10 个帖子),我们将要使用 query_posts() 的 [offset] 参数:

      // let's get the next 10 posts from category ID 3
      query_posts('posts_per_page=10&cat=3&offset=10');
      while(have_posts()):the_post();
          // do Wordpress magic right here
      endwhile;
      

      对于大多数用途,这应该足够了。但是,您确实提到您计划仅从主页对类别帖子列表进行分页?我假设您的意思是您的主页上有多个类别的帖子列表,并且所有这些都是独立分页的。

      有了类似的东西,看起来您必须使用 Javascript 来为您完成工作,以及我上面说明的内容。

      【讨论】:

        【解决方案3】:

        我相信你可以这样做:

            <?php
                if(isset($_GET['paged'])){
                    $page = $_GET['paged']-1;
                }else{
                    $page = 0;
                }
                $postsPerPage = 5;
                $theOffset = $page*$postsPerPage;
            ?>
            <?php query_posts(array('posts_per_page' => $postsPerPage, 'cat' => CATEGORIES HERE, 'offset' => $theOffset)); ?>
        

        【讨论】:

          【解决方案4】:

          希望对你有帮助:)

          <?php
             $args = array(
            'post_type' => 'post',
            'posts_per_page' => 5,
            'paged' => $page,
            );
          
            query_posts($args);?>
          ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-07-28
            • 2023-03-29
            • 2015-09-18
            • 1970-01-01
            • 2021-07-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多