当前位置:Gxlcms > PHP教程 > WordPress在首页使用query_posts()分页404?

WordPress在首页使用query_posts()分页404?

时间:2021-07-01 10:21:17 帮助过:32人阅读

小弟在开发时遇到个问题,思路大概是这样的:

  • 设置四种文章类型('post','detail','jingyan','haitao),相当于与后台的「文章」同级。

  • 在首页调取四种文章类型中的所有内容,如下:

 $notcat,
        'post__not_in' => $sticky,
        'post_type'=>array( 'post','detail','jingyan','haitao'),
        'posts_per_page' => 15,
        'paged' => $paged
    );
    query_posts( $args );
?>


...
  • Functions.php 加了这些:

set('post_type',$post_type);
        $query->set('posts_per_page', 15);
    return $query;
    }
}

问题来了: 按照这样的方法输出内容后,分页仅按照文章数显示。例如,「Post」下有 60 篇文章,「detail」、「jingyan」、「haitao」各 10 篇。按照想法应分 (60+10*3)/15 = 6 页。然而实际情况为翻至 60/15 = 4 页结束后,第 5、6 页均显示 404.

Copy 别人的 pagenavi()

request;
        $posts_per_page = intval(get_query_var('posts_per_page'));
        $paged = intval(get_query_var('paged'));
        $numposts = $wp_query->found_posts;
        $max_page = $wp_query->max_num_pages;
        if(empty($paged) || $paged == 0){
            $paged = 1;
        }
        $pages_to_show = intval($pagenavi_options['num_pages']);
        $pages_to_show = intval(4);
        $pages_to_show_minus_1 = $pages_to_show-1;
        $half_page_start = floor($pages_to_show_minus_1/2);
        $half_page_end = ceil($pages_to_show_minus_1/2);
        $start_page = $paged - $half_page_start;
        if($start_page <= 0) {
            $start_page = 1;
        }
        $end_page = $paged + $half_page_end;
        if(($end_page - $start_page) != $pages_to_show_minus_1) {
            $end_page = $start_page + $pages_to_show_minus_1;
        }
        if($end_page > $max_page) {
            $start_page = $max_page - $pages_to_show_minus_1;
            $end_page = $max_page;
        }
        if($start_page <= 0) {
            $start_page = 1;
        }
        if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
            $pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), "共 %TOTAL_PAGES% 页");
            $pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
            echo $before.''."\n";
            if(!empty($pages_text)) {
                echo ''.$pages_text.'';
            }                   
            if ($start_page >= 2 && $pages_to_show < $max_page) {
                $first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), "« 第一页");
                echo ''.$first_page_text.'';
                echo '...';
            }
            previous_posts_link("«");
            for($i = $start_page; $i  <= $end_page; $i++) {                       
                if($i == $paged) {
                    $current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), "%PAGE_NUMBER%");
                    echo ''.$current_page_text.'';
                } else {
                    $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), "%PAGE_NUMBER%");
                    echo ''.$page_text.'';
                }
            }
            next_posts_link("»", $max_page);
            if ($end_page < $max_page) {
                echo ''."...".'';
                $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), "最后 »");
                echo '最后 »';
            }
            echo ''.$after."\n";
        }
    }
}
 
?>

回复内容:

小弟在开发时遇到个问题,思路大概是这样的:

  • 设置四种文章类型('post','detail','jingyan','haitao),相当于与后台的「文章」同级。

  • 在首页调取四种文章类型中的所有内容,如下:

 $notcat,
        'post__not_in' => $sticky,
        'post_type'=>array( 'post','detail','jingyan','haitao'),
        'posts_per_page' => 15,
        'paged' => $paged
    );
    query_posts( $args );
?>


...
  • Functions.php 加了这些:

set('post_type',$post_type);
        $query->set('posts_per_page', 15);
    return $query;
    }
}

问题来了: 按照这样的方法输出内容后,分页仅按照文章数显示。例如,「Post」下有 60 篇文章,「detail」、「jingyan」、「haitao」各 10 篇。按照想法应分 (60+10*3)/15 = 6 页。然而实际情况为翻至 60/15 = 4 页结束后,第 5、6 页均显示 404.

Copy 别人的 pagenavi()

request;
        $posts_per_page = intval(get_query_var('posts_per_page'));
        $paged = intval(get_query_var('paged'));
        $numposts = $wp_query->found_posts;
        $max_page = $wp_query->max_num_pages;
        if(empty($paged) || $paged == 0){
            $paged = 1;
        }
        $pages_to_show = intval($pagenavi_options['num_pages']);
        $pages_to_show = intval(4);
        $pages_to_show_minus_1 = $pages_to_show-1;
        $half_page_start = floor($pages_to_show_minus_1/2);
        $half_page_end = ceil($pages_to_show_minus_1/2);
        $start_page = $paged - $half_page_start;
        if($start_page <= 0) {
            $start_page = 1;
        }
        $end_page = $paged + $half_page_end;
        if(($end_page - $start_page) != $pages_to_show_minus_1) {
            $end_page = $start_page + $pages_to_show_minus_1;
        }
        if($end_page > $max_page) {
            $start_page = $max_page - $pages_to_show_minus_1;
            $end_page = $max_page;
        }
        if($start_page <= 0) {
            $start_page = 1;
        }
        if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
            $pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), "共 %TOTAL_PAGES% 页");
            $pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
            echo $before.''."\n";
            if(!empty($pages_text)) {
                echo ''.$pages_text.'';
            }                   
            if ($start_page >= 2 && $pages_to_show < $max_page) {
                $first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), "« 第一页");
                echo ''.$first_page_text.'';
                echo '...';
            }
            previous_posts_link("«");
            for($i = $start_page; $i  <= $end_page; $i++) {                       
                if($i == $paged) {
                    $current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), "%PAGE_NUMBER%");
                    echo ''.$current_page_text.'';
                } else {
                    $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), "%PAGE_NUMBER%");
                    echo ''.$page_text.'';
                }
            }
            next_posts_link("»", $max_page);
            if ($end_page < $max_page) {
                echo ''."...".'';
                $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), "最后 »");
                echo '最后 »';
            }
            echo ''.$after."\n";
        }
    }
}
 
?>

人气教程排行