下午好
我正在为网站添加分页功能,目前工作正常,但由于某些原因,该功能会加载一个额外的页面。我们有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\' => __(\'«\'),
\'next_text\' => __(\'»\'),
\'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>";
}
}
我相信问题出在自定义分页功能中,但我无法定位它并终身修复它。非常感谢您的帮助!