這可能是因爲你的職位查詢調用setup_postdata
,使<!--nextpage-->
已經取代你有機會之前,所以你可能需要使用另一個過濾器或找出WordPress是插入。如果您使用get_posts而不是WP_query
,則可以在the_content
之前獲得setup_postdata
。理想情況下,您可以在此之前找到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);