WordPress静态页面分页

时间:2013-12-17 作者:Youssef Subehi

我在wordpress中使用了一个post查询,但分页不起作用,我不知道问题出在哪里,但这是我的代码,我想它是正确的,没有问题

它显示有多个页面,但当我单击下一页时,它会刷新页面,并且不会显示任何新结果,只显示同一页。

我在静态页面上使用它作为我主题的主页

<?php

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

$post_query = query_posts(array(
    \'post_type\'      => \'cover\', // You can add a custom post type if you like
    \'paged\'          => $paged,
    \'posts_per_page\' => 1
));

?>

<?php if ( have_posts() ) : ?>

<?php
while ( have_posts() ) : the_post(); 
?>

<?php endwhile; ?>

///Pagination Function in Functions.php
<?php my_pagination(); ?>

<?php else: ?>

    No Results

<?php endif; ?>

Pagination Function

if ( ! function_exists( \'my_pagination\' ) ) :
    function my_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
        ) );
    }
endif;

2 个回复
SO网友:ewroman

需要修改此解决方案,因为分页函数位于函数中。php。

我使用的是Revire主主题(它使用的是基础框架),该主题使用的是函数中的分页函数。php

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

    $big = 999999999; // This needs to be an unlikely integer

    // For more options and info view the docs for paginate_links()
    // http://codex.wordpress.org/Function_Reference/paginate_links
    $paginate_links = paginate_links( array(
        \'base\' => str_replace( $big, \'%#%\', get_pagenum_link($big) ),
        \'current\' => max( 1, get_query_var(\'paged\') ),
        \'total\' => $wp_query->max_num_pages,
        \'mid_size\' => 5,
        \'prev_next\' => True,
        \'prev_text\' => __(\'&laquo;\'),
        \'next_text\' => __(\'&raquo;\'),
        \'type\' => \'list\'
    ) );

    // Display the pagination if more than one page is found
    if ( $paginate_links ) {
        echo \'<div class="pagination-centered">\';
        echo $paginate_links;
        echo \'</div><!--// end .pagination -->\';
        }
    }
}
我已将此函数修改为

    if( ! function_exists( \'reverie_pagination\' ) ) {
    function reverie_pagination() {
        global $wp_query, $another_query;

        $big = 999999999; // This needs to be an unlikely integer
        if ( is_front_page()) {
            $myqueryis = $another_query;
            $paged = ( get_query_var(\'page\') ) ? get_query_var(\'page\') : 1;
            } else {
            $myqueryis = $wp_query;
            $paged = get_query_var(\'paged\');
            }
        // For more options and info view the docs for paginate_links()
        // http://codex.wordpress.org/Function_Reference/paginate_links
        $paginate_links = paginate_links( array(
            \'base\' => str_replace( $big, \'%#%\', get_pagenum_link($big) ),
            \'current\' => max( 1, $paged ),
            \'total\' => $myqueryis->max_num_pages,
            \'mid_size\' => 5,
            \'prev_next\' => True,
            \'prev_text\' => __(\'&laquo;\'),
            \'next_text\' => __(\'&raquo;\'),
            \'type\' => \'list\'
        ) );

        // Display the pagination if more than one page is found
        if ( $paginate_links ) {
            echo \'<div class="pagination-centered">\';
            echo $paginate_links;
            echo \'</div><!--// end .pagination -->\';
        }
    }
}
变量$another\\U query是我的自定义WP\\U查询。此问题作者使用*query\\u posts*获得结果,但我使用*new WP\\u query*。

我在头版中使用的查询是;

 $args =  array(
            \'post_type\' => \'post\',  
            \'post__not_in\'   => $do_not_duplicate,
        \'paged\' => $paged,
            );
 $another_query = new WP_Query( $args );

SO网友:ewroman

如果在同一页中有多个循环(例如,在front\\u页中),有时分页会被破坏。

我使用相同的查询名称解决了这个问题;根据以上示例,我们使用$myqueryis = $another_query; 在我们使用的函数中\'total\' => $myqueryis->max_num_pages, 并给出了查询参数和如下查询;

$args =  array(
    \'post_type\' => \'post\',  
    \'post__not_in\'   => $do_not_duplicate,
    \'paged\' => $paged,
        );
 $another_query = new WP_Query( $args );
第二个循环示例

$secondargs =  array(
        \'post_type\' => \'post\',  
        \'post__not_in\'   => $do_not_duplicate,
        \'paged\' => $paged,
        \'posts_per_page\' => 2,  // for testing purpose to see if pagination works properly
            );
 $another_query = new WP_Query( $secondargs );
也许还有第三个循环的例子

$thirdargs =  array(
        \'post_type\' => \'post\',  
        \'post__not_in\'   => $do_not_duplicate,
        \'paged\' => $paged,
        \'posts_per_page\' => 5,  // for testing purpose to see if pagination works properly
            );
 $another_query = new WP_Query( $thirdargs );
对于第/1页/第/2页/…,此操作正常。。。

结束

相关推荐

Pagination of RSS2 feed

如何对RSS2提要进行分页?我当前的RSS链接类似于:http://www.example.com/?feed=rss2 我试过:http://www.example.com/?feed=rss2&page=2 但是WordPress仍然返回相同的结果集,因为我在仪表板>设置>阅读>联合提要显示最新的(当然我有10多个帖子)