Pagination custom query

时间:2014-07-12 作者:justinw

我正在尝试制作一个自定义页面模板,以显示浏览次数最多的帖子。我可以回复帖子,但我很难弄清楚如何分页。以下是我所拥有的:

$args = array(\'orderby\' => \'meta_value_num\', \'meta_key\' => \'post_views_count\', \'posts_per_page\' => 36 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
我试过:

$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1; 
            $args = array(\'paged\' => $paged, \'orderby\' => \'meta_value_num\', \'meta_key\' => \'post_views_count\', \'posts_per_page\' => 36 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
但我的分页仍然没有显示。我想知道这是否与我的分页功能或我设置查询的方式有关。

我只调用查询下面的分页函数,结果如下所示:

<?php
            $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1; 
            $args = array(\'paged\' => $paged, \'orderby\' => \'meta_value_num\', \'meta_key\' => \'post_views_count\', \'posts_per_page\' => 36 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();?>
                <?php get_template_part( \'content\', get_post_format() ); ?>
            <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>

<?php pagination(); ?>
这是我的分页功能:

if ( ! function_exists( \'pagination\' ) ) :
    function pagination() {
        global $wp_query;

        $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\' => $wp_query->max_num_pages,
            \'mid_size\' => 1,
            \'prev_text\'    => __(\'«\'),
            \'next_text\'    => __(\'»\'),
            \'type\'         => \'list\'
        ) );
    }
endif;
任何帮助都将不胜感激。

3 个回复
最合适的回答,由SO网友:Shazzad 整理而成

这是一个局部/全局变量问题。可以通过将分页函数更改为使用局部变量来解决此问题,也可以通过将局部变量提升到全局范围来解决此问题。当您使用时wp_reset_postdata(), 我想您希望保留原始查询。

更改分页函数以接受参数-

if ( ! function_exists( \'pagination\' ) ) :

    function pagination( $paged = \'\', $max_page = \'\' ) {
        $big = 999999999; // need an unlikely integer
        if( ! $paged ) {
            $paged = get_query_var(\'paged\');
        }

        if( ! $max_page ) {
            global $wp_query;
            $max_page = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
        }

        echo paginate_links( array(
            \'base\'       => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
            \'format\'     => \'?paged=%#%\',
            \'current\'    => max( 1, $paged ),
            \'total\'      => $max_page,
            \'mid_size\'   => 1,
            \'prev_text\'  => __( \'«\' ),
            \'next_text\'  => __( \'»\' ),
            \'type\'       => \'list\'
        ) );
    }
endif;
循环模板如下-
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1; 
$args = array(
    \'paged\'          => $paged, 
    \'orderby\'        => \'meta_value_num\', 
    \'meta_key\'       => \'post_views_count\', 
    \'posts_per_page\' => 36 
);
$loop = new WP_Query( $args );

if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : 
        $loop->the_post();
        get_template_part( \'content\', get_post_format() );
    endwhile;

    pagination( $paged, $loop->max_num_pages); // Pagination Function
endif;

wp_reset_postdata();

SO网友:Abhik

试试这个。。

$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;
$args = array(
    \'orderby\' => \'meta_value_num\',
    \'meta_key\' => \'post_views_count\',
    \'posts_per_page\' => 36,
    \'paged\' => $paged
    );

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : $loop->the_post();

            get_template_part( \'content\', get_post_format() );

    endwhile;

    pagination(); // Pagination Function

endif;
wp_reset_postdata();
分页应添加在wp_reset_postdata();.

SO网友:Sayedul Sayem

我面对这个问题,就这样解决了

分页功能

function post_pagination($paged = \'\', $max_page = \'\') {
    if (!$paged) {
        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : ((get_query_var(\'page\')) ? get_query_var(\'page\') : 1);
    }

    if (!$max_page) {
        global $wp_query;
        $max_page = isset($wp_query->max_num_pages) ? $wp_query->max_num_pages : 1;
    }

    $big  = 999999999; // need an unlikely integer

    $html = paginate_links(array(
        \'base\'       => str_replace($big, \'%#%\', esc_url(get_pagenum_link($big))),
        \'format\'     => \'?paged=%#%\',
        \'current\'    => max(1, $paged),
        \'total\'      => $max_page,
        \'mid_size\'   => 1,
        \'prev_text\'  => __(\'« Prev\'),
        \'next_text\'  => __(\'Next »\'),
    ));

    $html = "<div class=\'navigation pagination\'>" . $html . "</div>";

    echo $html;
}
查询自定义模板
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : ((get_query_var(\'page\')) ? get_query_var(\'page\') : 1);

$args = array(
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\',
    \'posts_per_page\' => get_option(\'posts_per_page\'),
    \'order\' => \'DESC\',
    \'orderby\' => \'date\',
    \'paged\' => $paged
);

$loop = new WP_Query($args);
if ($loop->have_posts()) :
    while ($loop->have_posts()) :
        $loop->the_post();
        get_template_part(\'content\', get_post_format());
    endwhile;

    post_pagination($paged, $loop->max_num_pages);
endif;

wp_reset_postdata();

结束

相关推荐

Pagination and Related Posts

是否可以对嵌入在single中的以下相关POST代码使用分页。我的主题的php。 <?php // for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { $first_tag = $