我正在构建一个wordpress主题,并且分页不起作用,如果我使用自定义页面模板(带有自定义帖子查询)在显示帖子中显示良好和分页作品,但如果我将该页面设置为主题选项的主页或自定义程序选项分页不起作用 .

我一直在努力解决这个问题,请高度赞赏任何建议或帮助 .

<?php

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

    $args = array(
        'posts_per_page' => 2,
        'paged' => $paged
    );

    $the_query = new WP_Query($args);

?>
<?php if( $the_query->have_posts() ) : ?>
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <div class="main-post-loop">
            <div class="big-thum-section img-is-responsive">
                <?php if ( has_post_thumbnail() ) : ?>
                    <?php the_post_thumbnail('full'); ?>
                        <?php endif; ?>
            </div>
            <div class="squiggle-post-meta-section clearfix">
                <h2>
            <a href="<?php echo get_permalink(); ?>">
                <?php the_title(); ?> </a>
        </h2>
                <div class="excerpt-post">
                    <?php the_excerpt(); ?>
                </div>
            </div>
            <div class="continue-reading-section"> <a href="<?php echo get_permalink(); ?>" class="cont-reading"> Continue reading <i class="fa fa-chevron-right"></i></a> </div>
            <div class="squiggly-line"></div>
        </div>
        <!-- /.options -->
        <?php wp_reset_postdata(); ?>
            <?php endwhile;  ?>
                <?php else:  ?>
                    <p>
                        <?php _e( 'Sorry, no posts matched your criteria.' ); ?>
                    </p>
                    <?php endif; ?>
                        <!-- pagination -->
                        <div class="pagination">
                            <?php if (function_exists("pagination")) {
pagination($the_query->max_num_pages);
} ?>
                        </div>

我将以下代码放在我的主题function.php文件中 .

function pagination($pages = '', $range = 4)
{  
 $showitems = ($range * 2)+1;  

 global $paged;
 if(empty($paged)) $paged = 1;

 if($pages == '')
 {
     global $wp_query;
     $pages = $wp_query->max_num_pages;
     if(!$pages)
     {
         $pages = 1;
     }
 }   

 if(1 != $pages)
 {
     echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
     if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
     if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";

     for ($i=1; $i <= $pages; $i++)
     {
         if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
         {
             echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
         }
     }

     if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
     echo "</div>\n";
 }
}