You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress获取内容时如何让<!--nextpage-->分页标记生效?

解决WordPress中get_the_content_by_id不识别<!--nextpage-->分页的问题

get_the_content_by_id()函数仅会直接获取文章原始内容,不会触发WordPress内置的分页处理逻辑,因此无法识别<!--nextpage-->标记。要让分页规则生效,应该借助WordPress的文章上下文初始化机制,具体方案如下:

正确实现方式

通过get_post()获取目标文章对象,再用setup_postdata()初始化全局文章上下文,之后使用the_content()或get_the_content()输出/获取内容,WordPress会自动处理分页标记:

$mobile_content = 2525; // 目标文章/页面ID
$target_post = get_post($mobile_content);

if ($target_post) {
    // 初始化全局文章数据,让后续函数能识别当前文章上下文
    setup_postdata($target_post);
    
    // 自动根据URL中的page参数输出对应分页内容,同时处理所有内容过滤器(包括短代码、格式转换)
    the_content();
    
    // 重置文章数据,避免影响后续的查询或内容输出
    wp_reset_postdata();
}

如果需要获取处理后的内容字符串而非直接输出,可以用get_the_content()替代the_content():

$mobile_content = 2525;
$target_post = get_post($mobile_content);

if ($target_post) {
    setup_postdata($target_post);
    $processed_content = get_the_content();
    wp_reset_postdata();
    echo $processed_content;
}

方案优势

setup_postdata()会将目标文章对象绑定到全局$post变量上,当调用the_content()或get_the_content()时,会触发the_content过滤器链,其中就包含了处理<!--nextpage-->标记的拆分逻辑。

相比你手动用explode()拆分的方式,这个方案不仅能自动处理分页,还能兼容WordPress的内容过滤机制(比如解析短代码、自动添加段落标签),同时支持配合wp_link_pages()生成标准的分页导航,是更符合WordPress规范的实现方式。

内容的提问来源于stack exchange,提问作者Edu Rafael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 06:45:33