您的帖子查询很可能正在调用setup_postdata
,因此<!--nextpage-->
在您有机会之前已经被替换,因此您可能必须使用另一个过滤器或找出 Wordpress 插入的内容。如果您使用get_poststhe_content
而不是. 理想情况下,您可以在此之前找到一个过滤器,但不是在 DB 写入之前,但我似乎找不到任何工作要做。setup_postdata
WP_query
the_content
它并不漂亮,因为它是破坏性的(在保存到数据库之前替换标签)而不是在打印之前,但这可能对你有用:
function reformat_lists($content){
$f = '<!--nextpage-->';
$r = '<div id="cmn-list">';
$content = str_ireplace($f,$r,$content); //don't forget to pass your replaced string to $content
return $content;
}
add_filter( 'content_save_pre', 'reformat_lists');
编辑:更好的是,如果你得到global $post
,你可以抓取未过滤的内容。试试下面的 - 我</div>
在内容的末尾添加了一个来关闭我们插入的内容,这样它就不会破坏你的布局。抓取global $post
可能并非在所有情况下都有效,因此我将在您的设置中进行测试。
function reformat_lists($content){
global $post;
$content = $post->post_content;
$f = '<!--nextpage-->';
$r = '<div id="cmn-list">';
$content = str_ireplace($f,$r,$content) . '</div>';
return $content;
}
add_filter( 'the_content', 'reformat_lists', 1);