2013-07-17 89 views
1

在wordpress中,我沿着短代碼的內容輸出了一些<div>。問題是因爲wordpress將<p></p>標籤中的段落放在<div>所在的位置。 我想要實現的是用<br />替代<p> </p>,其圍繞我的短代碼。 例如,如果我的內容是這樣的:替換<p></p>與<br />條件

<p>[shortcodes attr='somethig']value[/shortcode] text<br /> 
[shortcodes attr='somethig']value[/shortcode] text<br /> 
text<br /> 
text [shortcodes attr='somethig']value[/shortcode] text </p> 
<p>text<br /> 
text [shortcodes attr='somethig']value[/shortcode] text<br /> 
text</p> 
<p> text without shortcode </p> 
<p>text [shortcodes attr='somethig']value[/shortcode] text </p> 

我需要將其轉換在

<br/>[shortcodes attr='somethig']value[/shortcode] text<br /> 
[shortcodes attr='somethig']value[/shortcode] text<br /> 
text<br /> 
text [shortcodes attr='somethig']value[/shortcode] text <br /> 
<br/>text<br /> 
text [shortcodes attr='somethig']value[/shortcode] text<br /> 
text<br/> 
<p> text without shortcode </p> 
<br/>text [shortcodes attr='somethig']value[/shortcode] text <br/> 

我嘗試自己解決問題,但我的preg_replace表達拍攝的全部內容,而不是個人<p> </p>塊。

解決方案使用jQuery是好的太(但不是簡碼,與特定的類 <div>必須在該段包含)

LE:下面的jQuery的工作。不過,我仍然在等待PHP中更優雅的解決方案。

$('p').each(function() { 
    if ($(this).next().find('.measurement_value').length > 0) 
    { 
     $(this).replaceWith( '<br />' + $(this).html()); 
    } 
}); 
+2

「WordPress的往裏面'

'標記段落」,並稱remove_filter('the_content', 'wpautop');擺脫<p> ??? – marekful

+3

這是在輸出內容之前在服務器端應該解決的問題。 – adeneo

+0

這就是我試圖在PHP中實現的內容,過濾內容;) –

回答

1

試試這個jQuery代碼:

$('p').each(function() { 
    if ($(this).find('.class-of-the-div').length > 0) { 
     $(this).replaceWith($(this).html() + '<br />'); 
    } 
}); 

但是,通常情況下,WordPress的與<p></p>在服務器端替換<br />

+0

謝謝,但這部分幫助我。我只想替換包含某個類的段落,而不是所有的段落。 –

+0

好的。我編輯了我的答案。 –

+0

由於該段在div之前的DOM中自動關閉,因此以下代碼工作: '$('p')。each(function(){if($(this).next()。 '.measurement_value ')長度> 0) \t \t { \t \t \t $(本).replaceWith('
' + $(本)的.html()); \t \t} \t});' 感謝您的幫助:) –

0

你爲什麼不只是獲得通過的functions.php

+0

我正在開發一個插件,我儘量避免在整個網站上產生影響。除此之外,該帖子變得混亂:) –