Pagination: Last Page Missing

时间:2016-10-12 作者:TheBigK

我有大约12张唱片,这意味着我应该有4页。到目前为止,我最大的努力是让分页工作正常进行,如果有人能告诉我我到底出了什么问题,我将不胜感激?

    <div id="main-content">
    <div class="container">

        <?php

            $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
            $args = array(
                \'post_type\' => \'job\',
                \'posts_per_page\' => 3,
                \'paged\' => $paged
            );
            $query = new WP_Query( $args );
            while ( $query->have_posts()): $query->the_post();
                <h1> <?php the_title(); ?> </h1>
                <p> <?php  the_excerpt(); ?> </p>
                <?php
            endwhile; ?>

    </div> <!-- .container -->
</div> <!-- #main-content -->

<!-- Start Navigation Here -->
<?php
    global $wp_query;

    $total_pages    =   $wp_query->max_num_pages;

    if ($total_pages > 1) {

        $current_page = max( 1, get_query_var(\'paged\'));
        echo \'<div class="page_nav">\';
        echo paginate_links(array(
            \'base\' => get_pagenum_link(1) . \'%_%\',
            \'format\' => \'/page/%#%\',
            \'current\' => $current_page,
            \'total\' => $total_pages,
            \'prev_text\' => \'Prev\',
            \'next_text\' => \'Next\'
        ));
        echo \'</div>\';
    }
?>

<?php wp_reset_postdata(); ?>
更新的代码:

好的,这是非常简化的,但无论我设置了多少“每页posts\\u”计数,它都会继续显示5页。总共有24条记录,我预计只有2页(又名max\\u num\\u pages),但我一直得到“5”。以下是我的最新简化代码:

        //Generate the loop here

        //Prepare arguments for WP_QUERY
        $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
        $args   = array(

            \'posts_per_page\'    => 12,
            \'post_type\'         => \'job\',
            \'paged\'             => $paged

        );

    $query  = new WP_Query( $args );

    if ($query->have_posts()) {

        while ( $query->have_posts()) {
            $query->the_post();
            ?> <li><?php the_title(); ?></li> <?php
        }
    } else {
        echo "<h2>No Jobs Found</h2>";
    }
    // Pagination begins here
    $paginateArgs = array(
        \'base\'  => \'%_%\',
        \'format\' => \'?paged=%#%\',
        \'current\'   => $paged
    );
    echo paginate_links( $paginateArgs );
    wp_reset_postdata();
    ?>

2 个回复
SO网友:fcastillo

也许,对于分页,您使用的是全局WP var$wp_query, 在posts_per_page 是全局开启WP设置。

我想你会用你的$query var也用于分页。

SO网友:TheBigK

事实证明,添加$big=99999999,然后在$paginateArgs中更改“base”就成功了。我仍然不知道它为什么会起作用;但确实如此。直接从CODEX获取代码。

 <?php

            //Generate the loop here

            //Prepare arguments for WP_QUERY
            $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
            $args   = array(

                \'posts_per_page\'    => 10,
                \'post_type\'         => \'job\',
                \'paged\'             => $paged

            );

        $query  = new WP_Query( $args );

        if ($query->have_posts()) {

            while ( $query->have_posts()) {
                $query->the_post();
                ?> <li><?php the_title(); ?></li> <?php
            }
        } else {
            echo "<h2>No Jobs Found</h2>";
        }
        // Pagination begins here
        $big = 999999999; // need an unlikely integer
        $paginateArgs = array(
            \'base\'  => str_replace( $big, \'%#%\', esc_url (get_pagenum_link( $big )) ),
            \'format\' => \'/page/%#%\',
            \'current\'   => $paged,
            \'total\' => $query->max_num_pages
        );
        echo paginate_links( $paginateArgs );
        wp_reset_postdata();
        ?>

相关推荐

change pagination url

目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?