簡體   English   中英

WordPress的短代碼關閉添加p標簽

[英]Wordpress short code closing p tags getting added

我正在嘗試為我的wordpress主題創建一些短代碼,但是wordpress添加關閉p標簽時遇到了問題。

這是functions.php文件中的簡短代碼

function fixed_background( $atts, $content = null ) {
extract(shortcode_atts(array(
            "class" => '',
            "img_src" => '#',
                ), $atts));

$return = 
    '<div class="fixed-background-image-container">
      <div class="fixed-background-image"><span style="background-image: url('.$img_src.')" class="background-fixed"></span>                     
        <div class="block content-960 center-relative">' . do_shortcode($content) . '</div>
      </div>
    </div>
    <div class="clear"></div>';

return $return;

}

add_shortcode("fixed_background", "fixed_background");

這是html輸出的結果:

<div class="fixed-background-image-container">
  <div class="fixed-background-image"><span style="background-image: url(http://www.blahblahblah.co.uk/photo2.jpg)" class="background-fixed"></span>                     </p>
    <div class="block content-960 center-relative">
    </div></div>
  </p></div>
  <div class="clear"></div>

請注意,內部放置了2個p標簽,如何去除這些標簽?

我還使用以下代碼禁用了wordpress的自動格式設置功能:

function webtreats_formatter($content) {
$new_content = '';

/* Matches the contents and the open and closing tags */
$pattern_full = '{(\[raw\].*?\[/raw\])}is';

/* Matches just the contents */
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';

/* Divide content into pieces */
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

/* Loop over pieces */
foreach ($pieces as $piece) {
    /* Look for presence of the shortcode */
    if (preg_match($pattern_contents, $piece, $matches)) {

        /* Append to content (no formatting) */
        $new_content .= $matches[1];
    } else {

        /* Format and append to content */
        $new_content .= wptexturize(wpautop($piece));       
    }
}

return $new_content;
}

// Remove the 2 main auto-formatters
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

// Before displaying for viewing, apply this function
add_filter('the_content', 'webtreats_formatter', 99);
add_filter('widget_text', 'webtreats_formatter', 99);

附注:順便說一句,我是否仍然可以整理編碼,請參見示例:

<div class="blahblah">
<div class="dv2">
<div class="dv3">
</div></div></div>

但它更像

<div class="blahblah">
  <div class="dv2">
    <div class="dv3">
    </div>
  </div>
</div>

您可以嘗試使用以下代碼修復空的段落:

add_filter('the_content', 'fix_shortcode_empty_paragraphs');
function fix_shortcode_empty_paragraphs($content) {
    $array = array(
        '<p>[' => '[',
        ']</p>' => ']',
        ']<br />' => ']',
    );

    $content = strtr($content, $array);

    return $content;
}

而且,不要忘記刪除要禁用自動格式設置的當前修補程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM