使用wp_Query时是否进行分页?

时间:2017-01-27 作者:kidcoder SAVE UKRAINE

<!-- query -->
<?php
    $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
    $query = new WP_Query( array(
        \'category_name\' => \'investor-news\',
        \'posts_per_page\' => 2,
        \'paged\' => $paged
    ) );
?>

<?php if ( $query->have_posts() ) : ?>

<!-- begin loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>

    <h2><a href="<?php the_permalink(); ?>" title="Read"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
    <?php echo get_the_date(); ?>

<?php endwhile; ?>
<!-- end loop -->


<!-- WHAT GOES HERE?????? -->


<?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
<?php endif; ?>
我已经尝试过使用wp\\u查询函数在这个静态页面上实现分页,但没有成功。在这个脚本中有一条评论叫做WHAT GOES HERE?????。。。那么这里是什么?

这是一个静态页面,不是首页或帖子页面。

4 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

代替<!-- WHAT GOES HERE?????? --> 分页代码如下:

<div class="pagination">
    <?php 
        echo paginate_links( array(
            \'base\'         => str_replace( 999999999, \'%#%\', esc_url( get_pagenum_link( 999999999 ) ) ),
            \'total\'        => $query->max_num_pages,
            \'current\'      => max( 1, get_query_var( \'paged\' ) ),
            \'format\'       => \'?paged=%#%\',
            \'show_all\'     => false,
            \'type\'         => \'plain\',
            \'end_size\'     => 2,
            \'mid_size\'     => 1,
            \'prev_next\'    => true,
            \'prev_text\'    => sprintf( \'<i></i> %1$s\', __( \'Newer Posts\', \'text-domain\' ) ),
            \'next_text\'    => sprintf( \'%1$s <i></i>\', __( \'Older Posts\', \'text-domain\' ) ),
            \'add_args\'     => false,
            \'add_fragment\' => \'\',
        ) );
    ?>
</div>
WordPress附带一个方便的函数,名为paginate_links() 它起着很重的作用。在上面的示例中,自定义WP\\u查询对象$query 使用而不是全局$wp_query 对象

SO网友:suthanalley

此代码用于自定义查询分页。您可以按照以下步骤在WordPress中创建自己的分页。

 <?php
/**
* Template Name: Custom Page
*/
get_header(); ?>

<?php
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$args = array(
  \'posts_per_page\' => 4,
  \'paged\' => $paged
);
$custom_query = new WP_Query( $args );
?>
          <!----start-------->
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

<?php
   while($custom_query->have_posts()) :
      $custom_query->the_post();
?>
       <div>
        <ul>
         <li>
           <h3><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></h3>
        <div>
          <ul>
        <div><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(\'thumbnail\'); ?></a></div>
          </ul>
          <ul>
        <p><?php echo the_content(); ?></p>
          </ul>
        </div>
        <div>
          </li>
        </ul>
          </div> <!-- end blog posts -->
       <?php endwhile; ?>
      <?php if (function_exists("pagination")) {
          pagination($custom_query->max_num_pages);
      } ?>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
          <!----end-------->
        <?php get_footer();
参考号:https://www.wpblog.com/use-wp_query-to-create-pagination/

SO网友:iBazzo

您可以简单地使用内置的WP函数

<?
the_posts_pagination()
?>
通过自定义WP\\U查询,它可以很好地完成这项工作

SO网友:Christopher Sample

如果有人试图从短代码内部创建分页的wp\\U查询,请执行以下操作:


add_shortcode(\'wp_query_pagination_inside_shortcode\', \'my_shortcode_function_tag\');
if (!function_exists(\'my_shortcode_function_tag\')) {
    function my_shortcode_function_tag($atts)
    {
        ob_start(); // Turn output buffering on
        global $paged;
        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        $atts = shortcode_atts(
            array(
                \'post_type\' => \'post\',
                // whatever you like
            ),
            $atts
        );
        $args = array(
            \'posts_per_page\' => 3,
            \'paged\' => $paged,
            \'post_type\' => $atts[\'post_type\'],
            \'orderby\' => \'date\',
            \'order\' => \'DESC\',
        );
        $the_query = new WP_Query($args);

        if ($the_query->have_posts()) {
            while ($the_query->have_posts()) {
                $the_query->the_post();
                get_template_part(\'template-parts/loop\');
            } //end while

            $big = 999999999; // need an unlikely integer
            echo paginate_links(
                array(
                    \'base\' => str_replace($big, \'%#%\', esc_url(get_pagenum_link($big))),
                    \'format\' => \'?paged=%#%\',
                    \'current\' => max(
                        1,
                        get_query_var(\'paged\')
                    ),
                    \'total\' => $the_query->max_num_pages //$q is your custom query
                )
            );
        } // Pagination inside the endif
        return ob_get_clean(); // Silently discard the buffer contents
        wp_reset_query(); // Lets go back to the main_query() after returning our buffered content
    }
}

相关推荐

在Get_the_Posts_Pagination函数中编辑分页文本

我想在链接模板中编辑screen\\u reader\\u文本。php我可以在一个主题中这样做,这样它就不会在更新时被覆盖。看起来过滤器是最好的选择,但我找不到关于使用什么过滤器的文档。这是我想从链接模板更改的代码。php: if ( $GLOBALS[\'wp_query\']->max_num_pages > 1 ) { $args = wp_parse_args( $args, array( \'mid_size\' =&