以下是一种支持表单分页标题的方法:
<!--nextpage(.*?)?-->
以类似的方式作为核心支撑
<!--more(.*?)?-->
.
下面是一个示例:
<!--nextpage Planets -->
Let\'s talk about the Planets
<!--nextpage Mercury -->
Exotic Mercury
<!--nextpage Venus-->
Beautiful Venus
<!--nextpage Earth -->
Our Blue Earth
<!--nextpage Mars -->
The Red Planet
输出类似于:
这是在2016主题上测试的,我必须稍微调整一下填充和宽度:
.page-links a, .page-links > span {
width: auto;
padding: 0 5px;
}
演示插件
content_pagination
,
wp_link_pages_link
,
pre_handle_404
和
wp_link_pages_args
支持下一页标记扩展的过滤器(
PHP 5.4+):
<?php
/**
* Plugin Name: Content Pagination Titles
* Description: Support for <!--nextpage(.*?)?--> in the post content
* Version: 1.0.1
* Plugin URI: http://wordpress.stackexchange.com/a/227022/26350
*/
namespace WPSE\\Question202709;
add_action( \'init\', function()
{
$main = new Main;
$main->init();
} );
class Main
{
private $pagination_titles;
public function init()
{
add_filter( \'pre_handle_404\', [ $this, \'pre_handle_404\' ], 10, 2 );
add_filter( \'content_pagination\', [ $this, \'content_pagination\' ], -1, 2 );
add_filter( \'wp_link_pages_link\', [ $this, \'wp_link_pages_link\' ], 10, 2 );
add_filter( \'wp_link_pages_args\', [ $this, \'wp_link_pages_args\' ], PHP_INT_MAX );
}
public function content_pagination( $pages, $post )
{
// Empty content pagination titles for each run
$this->pagination_titles = [];
// Nothing to do if the post content doesn\'t contain pagination titles
if( false === stripos( $post->post_content, \'<!--nextpage\' ) )
return $pages;
// Collect pagination titles
preg_match_all( \'/<!--nextpage(.*?)?-->/i\', $post->post_content, $matches );
if( isset( $matches[1] ) )
$this->pagination_titles = $matches[1];
// Override $pages according to our new extended nextpage support
$pages = preg_split( \'/<!--nextpage(.*?)?-->/i\', $post->post_content );
// nextpage marker at the top
if( isset( $pages[0] ) && \'\' == trim( $pages[0] ) )
{
// remove the empty page
array_shift( $pages );
}
// nextpage marker not at the top
else
{
// add the first numeric pagination title
array_unshift( $this->pagination_titles, \'1\' );
}
return $pages;
}
public function wp_link_pages_link( $link, $i )
{
if( ! empty( $this->pagination_titles ) )
{
$from = \'{{TITLE}}\';
$to = ! empty( $this->pagination_titles[$i-1] ) ? $this->pagination_titles[$i-1] : $i;
$link = str_replace( $from, $to, $link );
}
return $link;
}
public function wp_link_pages_args( $params )
{
if( ! empty( $this->pagination_titles ) )
{
$params[\'next_or_number\'] = \'number\';
$params[\'pagelink\'] = str_replace( \'%\', \'{{TITLE}}\', $params[\'pagelink\'] );
}
return $params;
}
/**
* Based on the nextpage check in WP::handle_404()
*/
public function pre_handle_404( $bool, \\WP_Query $q )
{
global $wp;
if( $q->posts && is_singular() )
{
if ( $q->post instanceof \\WP_Post )
$p = clone $q->post;
// check for paged content that exceeds the max number of pages
$next = \'<!--nextpage\';
if ( $p
&& false !== stripos( $p->post_content, $next )
&& ! empty( $wp->query_vars[\'page\'] )
) {
$page = trim( $wp->query_vars[\'page\'], \'/\' );
$success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
if ( $success )
{
status_header( 200 );
$bool = true;
}
}
}
return $bool;
}
} // end class
安装:创建
/wp-content/plugins/content-pagination-titles/content-pagination-titles.php
文件并激活插件。在测试任何插件之前备份总是一个好主意。
如果缺少顶部下一页标记,则第一个分页标题为数字。
此外,如果缺少内容分页标题,即。<!--nextpage-->
, 然后它将是数字,正如预期的那样。
我第一次忘记了WP
类,如果我们通过content_pagination
滤器最近,@PieterGoosen在#35562.
我们尝试在演示插件中使用pre_handle_404
过滤器回调,基于WP
类别检查here, 我们检查的地方<!--nextpage
而不是<!--nextpage-->
.
测试下面是一些进一步的测试:
Test #1
<!--nextpage-->
Let\'s talk about the Planets
<!--nextpage-->
Exotic Mercury
<!--nextpage-->
Beautiful Venus
<!--nextpage-->
Our Blue Earth
<!--nextpage-->
The Red Planet
所选1的输出:
正如所料。
Test #2
Let\'s talk about the Planets
<!--nextpage-->
Exotic Mercury
<!--nextpage-->
Beautiful Venus
<!--nextpage-->
Our Blue Earth
<!--nextpage-->
The Red Planet
所选5的输出:
正如所料。
Test #3
<!--nextpage-->
Let\'s talk about the Planets
<!--nextpage Mercury-->
Exotic Mercury
<!--nextpage-->
Beautiful Venus
<!--nextpage Earth -->
Our Blue Earth
<!--nextpage Mars -->
The Red Planet
所选3的输出:
正如所料。
Test #4
Let\'s talk about the Planets
<!--nextpage Mercury-->
Exotic Mercury
<!--nextpage Venus-->
Beautiful Venus
<!--nextpage Earth -->
Our Blue Earth
<!--nextpage Mars -->
The Red Planet
选择接地时的输出:
正如所料。
另一种方法是对其进行修改,以支持添加分页标题:
<!--pt Earth-->
还可以方便地为所有分页标题(pts)支持一条注释:
<!--pts Planets|Mercury|Venus|Earth|Mars -->
或者通过自定义字段?