2012-09-11 51 views
1

我想修改一個Wordpress短標籤的佈局。我想要修改的標籤是將一個帖子分成多個頁面的標籤。禁用Wordpress短標籤

在我的主題中,我需要禁用該功能並在div中包裝每個部分。

我知道可以添加過濾器來修改短標籤,但顯然我做錯了什麼。下面的函數似乎並沒有取代短標籤,我仍然得到一個分頁列表。

任何人都可以提出一個解決方案,以取代短標籤?

add_filter('the_content', 'reformat_lists'); 

function reformat_lists($content){ 
    $f = '<!--nextpage-->'; 
    $r = '<div id="cmn-list">'; 
    str_replace($f,$r,$content); 
    return $content; 
} 

回答

0

這可能是因爲你的職位查詢調用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);