在WordPress中,如果你想要在文章内容中插入分页符以创建分页的效果,你可以使用以下代码:
<?php
// 在文章内容中插入分页符
$content = get_the_content();
$pages = explode( '<!--nextpage-->', $content );
foreach( $pages as $page ){
echo apply_filters( 'the_content', $page );
// 如果不是最后一页,则插入分页 HTML
if( $pages[1] != $page ){
echo '<div style="page-break-after: always;"></div>';
}
}
?>
这段代码首先获取文章内容,然后通过<!--nextpage-->
分割内容,得到一个页数组。遍历这个数组,并显示每一页的内容。如果不是最后一页,它会在内容之后插入一个div
,其样式属性设置为page-break-after: always
,这会告诉浏览器在显示这个div
后立即进行分页。
要在文章编辑界面中手动插入分页符,你只需要在你希望分页的地方插入<!--nextpage-->
即可。这个分页符不会在前端显示,但会在源代码中保留,以便后端处理。