更新2016-01-21
我目前所有的测试都是在新安装的4.4.1上完成的,并具有以下设置:
Plain permalinks
Twentysixteen Theme
No plugins activated
如果该帖子只有一页(即<!--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之前,我可以使用以下内容在多页帖子后附加一个页面:
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;
}
在这一点上,我对如何还原此功能还没有任何想法,我们将不胜感激。