【问题标题】:Wordpress Pagination - Paged returns Empty PageWordpress 分页 - 分页返回空页面
【发布时间】:2015-05-17 11:31:11
【问题描述】:

我有一个分页功能,可以正确显示页码,但是在单击下一页时,例如 2 它简单地显示一个白色的空白页,无论我做什么,我都尝试了所有可能的选项来至少向我显示错误,但什么都没有..我有一个分页功能,当我在短代码中使用它时,它工作得很好..但是在任何页面中,如果我在 GET 中将 page.php?page_id=1&paged=2 传递到 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 */
}

有人可以指出一件对我有帮助的事情吗?感谢您的宝贵时间。

问候

【问题讨论】:

  • 尝试启用 wp 调试并检查 error_log 是否有任何提示
  • 谢谢哈桑,不幸的是,我的 wp-config 中有它们.. error_reporting(E_ALL); ini_set('display_errors', 1);定义('WP_DEBUG', true);
  • 我尝试添加 die("this this text"); index.php 中的一行,它显示正常,但是如果我添加 &paged=2 它会显示空白页...但是 &paged=1 会显示此文本.. 我真的很累或为此而战,已经快一个星期了..
  • 您的主题中有 404.php 吗?听起来白页可能是因为它显示的是空的/损坏的 404 模板。
  • 天啊-内森。我怎么能把那个页面留空:(由于我在 WP 方面的知识有限,我已经一周试图找到了......谢谢一百万......现在我可以从那里继续......谢谢......

标签: wordpress pagination


【解决方案1】:

每页的默认帖子数为 10。您已在自定义查询中将其设置为 2,但此时 WordPress 已确定不需要第 2 页,而是显示 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 模板中删除您的自定义查询并使用常规循环。

如果唯一改变的是每页的帖子数量并且它应该在全局范围内应用,那么您最好在“设置”->“阅读”页面的管理面板中更改值。

【讨论】:

  • Nathan - YOU ROCK...您还解决了前面的问题,我会浪费更多时间寻找解决方案...非常感谢...非常感谢...干杯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
相关资源
最近更新 更多