問題描述

更新 2016-01-21

目前我所做的所有測試都是透過以下設定全新安裝 4.4.1:Plain permalinks Twentysixteen Theme No plugins activated

如果帖子只有 1 頁 (即<!--nextpage--> 沒有出現在帖子中),那麼額外的頁面將成功新增 (即使你附加了多個額外的頁面) 。

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

如果帖子有 2+頁面,那麼額外的頁面 404 和規範重定向到帖子的第 1 頁。

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

<!--nextpage-->

This is page 2

在第二種情況下,一旦你打了額外的頁面,$wp_query->queried_object 就是空的。您需要停用規範重定向才能看到此 remove_filter('template_redirect', 'redirect_canonical');

以下核心修補程式已經單獨和共同嘗試,行為沒有變化:https://core.trac.wordpress.org/ticket/35344#comment:16

https://core.trac.wordpress.org/ticket/35344#comment:34

為了方便使用,這是我正在測試的程式碼:

add_action('template_redirect', 'custom_content_one');
function custom_content_one() {
    global $post;
    $content = "n<!--nextpage-->nThis is the extra page v1";
    $post->post_content .= $content;
}

add_filter('content_pagination', 'custom_content_two', 10, 2);
function custom_content_two($pages, $post) {
    if ( in_the_loop() && 'post' === $post->post_type ) {
        $content = "This is the extra page v2";

        $pages[] = $content;
    }
    return $pages;
}

add_action('the_post', 'custom_content_three');
function custom_content_three() {
    global $multipage, $numpages, $pages;
    $content = "This is the extra page v3";

    $multipage = 1;
    $numpages++;
    $pages[] = $content;
}

¹這是我用來在單個頁面上測試多個額外頁面的程式碼

add_action('template_redirect', 'custom_content_one');
function custom_content_one() {
    global $post;
    $content = "n<!--nextpage-->nThis is the extra page v1-1n<!--nextpage-->nThis is the extra page v1-2n<!--nextpage-->nThis is the extra page v1-3";
    $post->post_content .= $content;
}

原始問題

在 4.4 之前,我可以新增一個額外的頁面到一個 muttipage 的帖子與以下:

add_action('template_redirect', 'custom_content');
function custom_content() {
    global $post;
    $content = html_entity_decode(stripslashes(get_option('custom_content')));
    $post->post_content .= $content;
}

使用 get_option(‘custom_content’) 就像:

<!--nextpage-->
Hello World

由於升級到 4.4,程式碼沒有起作用; 導航到附加頁面會觸發 404 錯誤,並將 redirect_canonical 傳送回該固定連結。停用 redirect_canonical 允許我檢視額外的頁面,並且附加內容在那裡,但它仍然觸發 404 錯誤。

我嘗試了一些解決方法,沒有一個解決 404 錯誤,包括:

add_action('the_post', 'custom_content');
function custom_content() {
    global $multipage, $numpages, $pages;
    $content = html_entity_decode(stripslashes(get_option('custom_content')));

    $multipage = 1; // ensure post is considered multipage: needed for single page posts
    $numpages++; // increment number of pages
    $pages[] = $content;
}

也嘗試利用 4.4 中新增的新的 content_pagination 過濾器:

add_filter('content_pagination', 'custom_content', 10, 2);
function custom_content($pages, $post) {
    $content = html_entity_decode(stripslashes(get_option('custom_content')));

    $pages[] = $content;
    return $pages;
}

在這一點上,我沒有想法如何恢復此功能,任何幫助將不勝感激。

最佳解決方案

更新 21-01-2016 19:35 SA 時間 – BUG FOUND !!!!! 是啊!!!!!!

我終於找到了這個 bug 。正如您在上次更新中所述,故障僅在 $post_content 在內容中具有<!--nextpage--> 標記時發生。我測試了它,並確認在<!--nextpage--> 返回 404 之後頁面之後的任何其他頁面,然後該頁面重定向回到第一頁。

這是由於 handle_404()中的 the following lines of code,它是在 WordPress 4.4 中的 WP 類中引入的

// check for paged content that exceeds the max number of pages
$next = '<!--nextpage-->';
if ( $p && false !== strpos( $p->post_content, $next ) && ! empty( $this->query_vars['page'] ) ) {
    $page = trim( $this->query_vars['page'], '/' );
    $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
}

這個程式碼是什麼,每當在 post_content 中設定<!--nextpage--> 標籤時,當透過 content_pagination 過濾器在內容後面訪問任何頁面時,它將返回 404 。由於設定了 404,redirect_canonical()將任何附加頁面重定向到第一頁

我已經提交了關於這個問題的 trac 門票,您可以在這裡檢視

在撰寫本文時,還沒有任何反饋,所以請務必定期檢視機票的狀態

當前解決方案 – A /W TRAC TICKET FEEDBACK

現在,直到我們在將來的版本中得到任何反饋和可能的修復,只需從 WP 類中刪除這些行,直到另行通知

什麼時候是… 它的調查時間!!!!!

我有時間充分測試這個。我把你的程式碼和測試:

  • 我的 v4.3 本地安裝

  • 我的 v4.4.0 本地安裝

  • 我的 v4.4.1 本地安裝

  • 完成新的 v4.4.1 本地安裝,只有 Hello World 的帖子和 Sample Page 頁面

與我的永久連結設定

  • default

  • Post Name

這是我的測試程式碼,在我的測試帖子裡面建立 4 頁。

add_filter('content_pagination', 'custom_content', 10, 2);
function custom_content($pages, $post) {
    $pages_to_add = [
        'Hello World Page 2',
        'Hello World Page 3',
        'Hello World Page 4',
    ];

    foreach ( $pages_to_add as $page_to_add ){
        $pages[]  = html_entity_decode(
            stripslashes(
                $page_to_add
            )
        );
    }

    return $pages;
}

我也測試過

add_filter('content_pagination', 'custom_content', 10, 2);
function custom_content($pages, $post) {
    $pages_to_add = [
        '<!--nextpage--> Hello World Page 2',
        '<!--nextpage--> Hello World Page 3',
        '<!--nextpage--> Hello World Page 4',
    ];

    foreach ( $pages_to_add as $page_to_add ){
        $pages[]  = html_entity_decode(
            stripslashes(
                $page_to_add
            )
        );
    }

    return $pages;
}

好的措施

在每個安裝和永久連結結構中,您的所有程式碼都可以工作 (除了預期的 v4.3 上的 content_pagination) 。

我也將 Sample Page 設定為靜態首頁,但是在第 2 頁上失敗,作為我的原始答案和**編輯中描述的錯誤的構造

所以結論是這與核心中的錯誤或核心中的任何其他錯誤無關。從評論中,有人在分頁的帖子頁面上取消了查詢物件,這就是我們需要除錯的東西。不幸的是,由於這個問題現在已經被本地化了,我無法給出確切的解決方案。

調查問題

您需要使用以下工作流來除錯問題

  • 給自己一大堆高咖啡,大量的糖

  • 備份你的 db

  • 下載並安裝以下外掛 (我沒有附屬於任何外掛)

    • Debug Objects 正常除錯。一旦安裝和安裝,修復外掛可能突出顯示的所有明顯的錯誤。如果您有明顯的錯誤,請不要繼續下一個主要的子彈點。先修好

    • DB Manager,您將用於修復和清理您的資料庫,然後再繼續下一個子彈點

  • 清除所有快取,瀏覽器和外掛

  • 停用所有外掛,並重新清除所有快取記憶體。因為這個問題看起來像一個重定向問題,我可能會先關閉所有可能與重定向有關的外掛。可能是一個外掛還不相容 v4.4 。檢查問題是否持續,如果是,繼續下一個專案符號,否則,讓我們更詳細地看看這一點開始,透過停用所有外掛,您也可以透過停用可能顯而易見導致問題的外掛來啟動。在啟用每個外掛後,正確測試安裝。導致這個問題的第一個外掛被啟用將成為罪魁禍首。在這種情況下,請使用除錯詳細資訊與外掛作者聯絡。只要確保在每次外掛啟用之後清除快取,才能很好的衡量

  • 如果達到這一點,以前的專案符號並沒有解決你的問題。下一步應該是切換到捆綁的主題,以消除您的主題為問題。再次,清除快取。

  • 如果一切都失敗了,您將再留下兩個選項

    • 刪除.htaccess,讓 WordPress 建立一個新的

    • 重新安裝 WordPress

這應該可以解決你的問題。如果沒有,您需要考慮可能導致該問題的 WordPress 核心中的錯誤。

我希望這有助於捕捉這個 bug

UPDATE

我應該實際上連線到這個似乎更詳細地解釋一切的火雞

有趣的和相當相關的補丁從上面的 trac 門票

我現在不能具體地測試任何東西,但你應該透過建議的補丁進行測試。我可以拿到的是,redirect_canonical()中負責靜態字首分頁的程式碼也是單頁分頁的責任。

原始答案

單頁 (如靜態首頁) 使用 get_query_var( 'page' )進行分頁。使用 WordPress 4.4(和 v4.4.1),當使用 get_query_var( 'page' )進行分頁時,出現了一個錯誤,導致分頁問題。

目前的錯誤報告,比如 trac ticket # 35365,只提到有分頁問題的靜態首頁,但是由於該 bug 與 get_query_var( 'page' )有關,我會認為這也會導致使用 get_query_var( 'page' )的單後分頁問題。

您應該嘗試在 trac 門票中描述的修補程式。如果這個工作,你可以應用補丁並等待 v4.4.2 這將修復這個錯誤

次佳解決方案

請注意,您提供的所有這三個示例都有一個語法錯誤:

add_filter('content_pagination', 'custom_content'), 10, 2);

add_action('the_post', 'custom_content'));

add_action('template_redirect', 'custom_content'));

其中新增了一個額外的)

將這些行替換為:

add_filter( 'content_pagination', 'custom_content', 10, 2);

add_action( 'the_post', 'custom_content' );

add_action( 'template_redirect', 'custom_content' );

我不會推薦一般地全域性物件,所以我認為你的最後一個例子與 content_pagination 過濾器是這裡的方式。

您可能還想避免使用以下方式附加空頁:

if( ! empty( $content ) )
    $pages[] = $content;

在這裡也有一個缺少的)

$content = html_entity_decode(stripslashes(get_option('custom_content'));

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。