在 post 分页之前自定义 post

Customizing the post before post pagination

我想在 post 分页之前执行一个脚本。

我也通过在数据分页之前执行脚本成功实现了这一点

if ( false !== strpos( $content, '<!--nextpage-->' ) )

wp-includes/query.php

我需要更改根据某些条件分页的 post 个页面。

假设

if(condition == true){
    $paged_data = explode('<!--nextpage-->', $content);
    $i = 0;
    $allpages = "";
    foreach($paged_data as $p_data){
        if( ($i) % 3 != 0){
            $allpages = $allpages.$p_data;
        }
        else{
            $allpages = $allpages . '<!--nextpage-->' .$p_data;
        }
        $i++;
    }
    $content = $allpages;
}

但我需要使用一些挂钩或过滤器来实现。 这样wordpress更新的时候代码就不会被删

我试过 the_content 钩子,但它在 post 分页后执行。

我什至试过 the_post 挂钩,但它只适用于少数主题。

这是一个示例,当使用 内容分页:

时,我们如何合并 页面

演示插件:

我们可以使用content_pagination修改内容分页:

/**
 * Content Pagination - Merge pages in chunks
 */
add_filter( 'content_pagination', function( $pages )
{
    // Nothing to do without content pagination
    if( count( $pages ) <= 1 )
        return $pages;

    // Number of pages per chunk    
    $pages_per_chunk = 3; // <-- Edit to your needs!

    // Create chunks of pages   
    $chunks = array_chunk( $pages, $pages_per_chunk );

    // Merge pages in each chunk
    $merged = [];
    foreach( (array) $chunks as $chunk )
        $merged[] = join( '', $chunk );

    return $merged;

} );

我们根据需要调整 $pages_per_chunk