第11页之后无效的帖子的自定义分页

时间:2017-02-14 06:00:05

标签: php wordpress post pagination

我在家中创建了一个部分,我从偏移3开始显示6个帖子,然后我添加了一个自定义分页代码。

我已经发布了93篇帖子,因为我设置了偏移量3,所以为其余的90个帖子设置了分页。

对于90个帖子,链接总数为15个。

分页工作正常到11页,即mysiteurl / page / 11。当我在分页链接中单击12时,它会显示404.它应该显示我接下来的6个帖子。

这是我的代码:

<?php
// function to get page number
function get_url_var($name){
    $strURL = $_SERVER['REQUEST_URI'];
    $arrVals = split("/",$strURL);
    $found = 0;
    foreach ($arrVals as $index => $value){
        if($value == $name) $found = $index;
    }
    $place = $found + 1;
    return $arrVals[$place];
}
function myprefix_query_offset(&$query) {
    //Before anything else, make sure this is the right query...
    if ( ! $query->is_home() ) {
        return;
    }
    //First, define your desired offset...
    $offset = 3;
    //Next, determine how many posts per page you want (we'll use WordPress's settings)
    $ppp = 6;
    $page = get_url_var('page');
    //Next, detect and handle pagination...
    if ( $page ) {
        //Manually determine page query offset (offset + current page (minus one) x posts per page)
        $page_offset = $offset + ( ($page-1) * $ppp );
        //Apply adjust page offset
        $query->set('offset', $page_offset );
    }
    else {
        //This is the first page. Just use the offset...
        $query->set('offset',$offset);
    }
}
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
    //Define our offset again...
    $offset = 3;
    //Ensure we're modifying the right query object...
    if ( $query->is_home() ) {
        //Reduce WordPress's found_posts count by the offset... 
        return $found_posts - $offset;
    }
    return $found_posts;
}
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
$page = get_url_var('page');
$paged = ( $page ) ? $page : 1;
if($paged == 1){
    $custom_args = array(
        'post_type' => 'post',
        'posts_per_page' => 6,
        'paged' => $paged,
        'offset' => 3,
        'post_status' => 'publish'
    );
}else{
    $custom_args = array(
        'post_type' => 'post',
        'posts_per_page' => 6,
        'paged' => $paged,
        'offset' => 3 + ( ($paged-1) * 6),
        'post_status' => 'publish'
    );
}
$wpb_ll_query = new WP_Query( $custom_args );
if ( $wpb_ll_query->have_posts() ) : while ( $wpb_ll_query->have_posts() ) : $wpb_ll_query->the_post();
?>
<div class="col-lg-4 col-sm-6 col-md-4 col-xs-6  rec-first1">
    <a href="
<?php the_permalink();?>">
        <div class="grid-latest-posts">
            <?php
if(has_post_thumbnail())
{
    the_post_thumbnail();
}
else
{
    ?>
    <img class="no-image" src="/wp-content/uploads/2017/02/no-image.png">
    <?php
}
$category_object = get_the_category(get_the_ID());
$category_name = $category_object[0]->name;
            ?>
            <label><?php echo $category_name;?></label>
        </div>
    </a>
    <div class="style-txt-nxt">
        <span> <a href="<?php the_permalink();?>">
            <?php
$string = get_the_title();
$post_title = (strlen($string) > 30) ? substr($string,0,30)."..." : $string;
            ?>
            <?php
echo $post_title;
            ?>
            </a></span>
        <p id="art-date2">
            <?php echo get_the_date(); ?></p>
        <div class="line1">
        </div>
        <h3>
            <a href="
<?php the_permalink();?>">read more</a>
        </h3>
    </div>
</div>
<?php
endwhile;
//wp_reset_postdata(); 
else : ?>
<p>
    <?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>    
</div>
</section>
<!-- Pagination for posts -->
<section>
    <div class="container">
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <!--<h3><a id="more_posts">Load More</a></h3>-->
                <?php
if (function_exists(custom_pagination)) {
    custom_pagination($wpb_ll_query->max_num_pages,"",$paged);
}
wp_reset_postdata();
                ?>
            </div>
        </div>
    </div>
</section>

我在function.php中定义了custom_pagination()函数

<?php 
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
    if (empty($pagerange)) {
        $pagerange = 2;
    }
    /**
    * This first part of our function is a fallback
    * for custom pagination inside a regular loop that
    * uses the global $paged and global $wp_query variables.
    * 
    * It's good because we can now override default pagination
    * in our theme, and use this function in default queries
    * and custom queries.
    */
    global $paged;
    if (empty($paged)) {
        $paged = 1;
    }
    if ($numpages == '') {
        global $wp_query;
        $numpages = $wp_query->max_num_pages;
        if(!$numpages) {
            $numpages = 1;
        }
    }
    /** 
    * We construct the pagination arguments to enter into our paginate_links
    * function. 
    */
    $pagination_args = array(
        'base'            => get_pagenum_link(1) . '%_%',
        'format'          => 'page/%#%',
        'total'           => $numpages,
        'current'         => $paged,
        'show_all'        => False,
        'end_size'        => 1,
        'mid_size'        => $pagerange,
        'prev_next'       => True,
        'prev_text'       => __('&laquo;'),
        'next_text'       => __('&raquo;'),
        'type'            => 'plain',
        'add_args'        => false,
        'add_fragment'    => ''
    );
    $paginate_links = paginate_links($pagination_args);
    if ($paginate_links) {
        echo "<nav class='custom-pagination'>";
        echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
        echo $paginate_links;
        echo "</nav>";
    }
}?>

任何人都可以建议我解决一些问题。

0 个答案:

没有答案