อัพเดท 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-1\n<!--nextpage-->\nThis is the extra page v1-2\n<!--nextpage-->\nThis is the extra page v1-3";
$post->post_content .= $content;
}
คำถามเดิม
ก่อนหน้า 4.4 ฉันสามารถต่อท้ายหน้าเพิ่มเติมไปยังโพสต์ mutlipage ด้วยสิ่งต่อไปนี้:
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;
}
ลองใช้ประโยชน์จากตัวกรองcontent_paginationใหม่ที่เพิ่มใน 4.4:
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;
}
ณ จุดนี้ฉันไม่มีแนวคิดในการกู้คืนฟังก์ชันการทำงานนี้และความช่วยเหลือใด ๆ จะได้รับการชื่นชม