Custom Pagination is Broken

时间:2017-10-25 作者:NeoLobster

下午好

我正在为网站添加分页功能,目前工作正常,但由于某些原因,该功能会加载一个额外的页面。我们有7篇文章,我将其设置为每页显示4篇文章。然而,问题是分页加载了第三个不显示帖子的页面。我只是想知道为什么会这样,我怎么才能解决它?

下面是我的意思的一个活生生的例子:

以下是我的帖子页面中的代码:

<?php
/**
 *
 * 
 * Date: 2017-08-31
 * Time: 11:30 AM
 */

    get_header();
?>


<!-- Site HERO Section -->
<?php include get_template_directory() . \'/sections/hero.php\'; ?>


<!-- Latest Updates -->
<div class="latest-updates flex flex-column flex-centered">

  <center><h2><?php echo get_field(\'latest_update_title\', get_option(\'page_for_posts\')); ?></h2></center>
  <p><?php echo get_field(\'latest_update_description\', get_option(\'page_for_posts\')); ?></p>

</div>


<!-- Blog Posts -->
<div id="postsContainer" class="blog-posts flex flex-column">
  <?php

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

    $query_args = array(
      \'post_type\' => \'post\',
      \'posts_per_page\' => 4,
      \'paged\' => $paged,
    );

    $the_query = new WP_Query( $query_args );

    if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

      get_template_part( \'template-parts/content\', get_post_type() );

    endwhile; endif;
    wp_reset_postdata();
  ?>

</div>

  <nav class=\'custom-pagination flex flex-centered\'>
    <?php echo custom_pagination(); ?>
  </nav>

<!-- TESTIMONIALS Section -->
<?php
if ( get_field( \'testimonials_section_visible\', get_option( \'page_for_posts\' ) ) ):
    include get_template_directory() . \'/sections/testimonials.php\';
endif;
?>



<!-- SERVICES Section -->
<?php
if ( get_field( \'services_section_visible\', get_option( \'page_for_posts\' ) ) ):
    include get_template_directory() . \'/sections/services.php\';
endif;
?>



<!-- START PROJECT Section -->
<?php
if ( get_field( \'start_project_section_visible\', get_option( \'page_for_posts\' ) ) ):
    include get_template_directory() . \'/sections/start-project.php\';
endif;
?>



<!-- CLIENT LOGOS Section -->
<?php
if ( get_field( \'client_logos_visible\', get_option( \'page_for_posts\' ) ) ):
    include get_template_directory() . \'/sections/clients.php\';
endif;
?>



<!-- GET IN TOUCH Section -->
<?php
if ( get_field( \'get_in_touch_section_visible\', get_option( \'page_for_posts\' ) ) ):
    include get_template_directory() . \'/sections/get-in-touch.php\';
endif;
?>



<!-- INSTAGRAM Section -->
<?php
if ( get_field( \'instagram_section_visible\', get_option( \'page_for_posts\' ) ) ):
    include get_template_directory() . \'/sections/instagram.php\';
endif;
?>

<!-- Footer Section -->
<?php
get_footer();
?>
下面是custom\\u分页函数的PHP代码:

/*
 * Custom Pagination
 */
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 quries
     * 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=\'reviews-pagination-links flex flex-centered\'>";
        echo $paginate_links;
        echo "</nav>";
    }
}
我相信问题出在自定义分页功能中,但我无法定位它并终身修复它。非常感谢您的帮助!

1 个回复
SO网友:Anson W Han

你的寻呼机很可能会得到所有帖子的数量,无论它们是否已发布。在查询中,应仅为已发布的帖子添加筛选器。

    $query_args = array(
        \'post_type\' => \'post\',
        \'posts_per_page\' => 4,
        \'paged\' => $paged,
        \'post_status\' = \'publish\', 
    );
然后,结果计数和寻呼机链接的数量应该与您实际看到的渲染内容相匹配。

结束

相关推荐

包含PHP文件的短代码,传递的各种参数要包含吗?

希望在WP内容中包含php文件。某些参数会因包含而有所不同。我想写一个包含短代码的代码。我do not 希望为每个包含文件编写不同的包含函数。短代码示例:[包括文件路径=\'/包括/狗。php“color=\'black\'size=\'large\'][包括文件路径=\'/includes/table.php“type=\'dogs\'number=\'5\'sort=\'desc\']函数包含文件后,所有其他参数将传递给THAT file. 这可能吗?此代码已关闭,除了获取包括的所有其他参数:// S