WordPress分页-分页返回空白页

学得很快

我有一个分页功能,可以正确显示页码,但是在单击下一页时,例如2,它简单地显示了一个白色的空白页,无论我做什么,我都尝试着做所有可能的选择以至少向我显示错误,但是什么也没有根本没有。我有一个分页功能,当我在短代码中使用它时,它可以完美地工作..但是在任何页面中,如果我在GET中的paged = 2处传递page.php?page_id = 1&paged = 2,它就会显示空白页面。 。

页面的结构是这样的:

index.php具有以下代码。

 get_header();  
 get_template_part( 'templates/_content' );
 get_footer();

在_content.php模板文件中,我有以下代码。

if(is_home() OR is_category()) {

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

    $posts_per_page = 2;
    $options = array(
      'post_type' => 'post',
      'posts_per_page' => $posts_per_page,
      'paged'          => $paged,
      'post_status'    => 'publish'
    );

    $wp_query = new WP_Query($options);
    $pages = $wp_query->max_num_pages;

    $custom_pagination = custom_pagination($pages,$posts_per_page);
    if($wp_query->have_posts()) : 
        while($wp_query->have_posts()) : the_post();
            get_template_part( 'templates/blog_archive_template' );     
        endwhile;
    else:
        echo '
            <h2 class="center">Not Found !</h2>
            <p class="center">Sorry, but you are looking for something that isn\'t here.</p>';
    endif; 

    echo $custom_pagination;

} else {    
  if(have_posts()) : while(have_posts()) : the_post();
  /* rest of html code */
}

有人可以指出对我有帮助的一件事。谢谢你的时间。

问候

内森·道森(Nathan Dawson)

每页的默认帖子数为10。您已在自定义查询中将其设置为2,但到那时WordPress已经确定不需要第二页,而是显示404模板。

您需要改为使用修改主查询pre_get_posts

例子:

/**
 * Modify the main query on the posts index or category 
 * page. Set posts per page to 2.
 *
 * @param object $query
 */
function wpse_modify_home_category_query( $query ) {

    // Only apply to the main loop on the frontend.
    if ( is_admin() || ! $query->is_main_query() {
        return false;
    } 

    // Check we're on a posts or category page.
    if ( $query->is_home() || $query->is_category() ) {
        $query->set( 'posts_per_page', 2 );
    }
}
add_action( 'pre_get_posts', 'wpse_modify_home_category_query' );

将其添加到模板中后,就可以functions.php删除自定义查询,index.php并使用常规循环。

如果唯一要更改的是每页的帖子数,并且该更改应全局应用,那么最好更改设置->阅读页面上管理面板中的值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章