覆盖默认分页符-使用外部内容部分,分页符在WP_Query::setup_postdata() 方法,当我们调用the_post() 消息灵通的
页面部分存储在全局$pages 数组并使用获取get_the_content() 作用
这一切都发生在the_content 过滤器应用于内容。
我们可以使用自己的get_the_content() 包装器。
假设我们在主题模板中有这样的内容:
// Display
the_content();
// Paging
wp_link_pages();
 下面是使用一些外部内容部分修改分页符的两种方法。
1) 修改子主题,我们将其替换为相应子主题中的:
// Setup our content parts
$parts = [ get_the_content(), \'Part Two\', \'Part Three\', \'Part Four\' ];
// Display
if( function_exists( \'get_the_content_part_wpse194155\' ) )
    echo apply_filters( \'the_content\', get_the_content_part_wpse194155( $parts ) );
else
    the_content();
// Paging
wp_link_pages();
 我们还可以使用
the_content 滤器示例:
! is_admin() && add_filter( \'the_content\', function( $content )
{
    if( ! in_the_loop() || ! function_exists( \'get_the_content_part_wpse194155\' ) )
        return $content;
   // Setup our content parts
    $parts = [ $content, \'Part Two\', \'Part Three\', \'Part Four\' ];
    // Display
    return get_the_content_part_wpse194155( $parts );
} );
 注意,这里我们只使用静态内容部分进行演示。一般来说,您希望每个帖子都有不同的内容部分。
我们的自定义包装器定义为:
/**
 * Modify globals to support page breaks between given content parts
 *
 * @param  array  $parts Array of content parts 
 * @return string|null   A single content part
 */
function get_the_content_part_wpse194155( array $parts )
{
    global $page, $pages, $multipage, $numpages;
    // No paging needed
    if( count( $parts ) <= 1 )
        return array_pop( $parts );
    // Init
    $out   = \'\';
    $break = \'<!--nextpage-->\';
    $page  = (int) get_query_var( \'page\' );
    // Loop - add page breaks between parts
    foreach( (array) $parts as $part )
        $out .= $part . $break;
    // Remove the last empty page break
    if( count( $parts ) > 0 )
        $out = mb_substr( $out, 0, -1 * strlen( $break ) );
    // Adjust the globals
    $pages     = explode( $break, $out );
    $page      = ( $page > count( $pages ) || $page < 1 ) ? 1 : $page;
    $numpages  = count( $pages );
    $multipage = 1;    
    return $pages[$page-1];
}