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

來源:互聯網
上載者:User
小弟在開發時遇到個問題,思路大概是這樣的:

  • 設定四種文章類型('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";        }    }} ?>
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.