【问题标题】:Wordpress short code closing p tags getting addedWordpress 短代码关闭 p 标签被添加
【发布时间】:2014-08-13 08:09:37
【问题描述】:

我正在尝试为我的 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);

P.S 顺便说一句,我是否可以整理代码,请参见示例:

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

但更像是

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

【问题讨论】:

    标签: php wordpress shortcode autoformatting


    【解决方案1】:

    您可以尝试使用以下代码来修复空段落:

    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;
    }
    

    并且不要忘记删除您正在应用的当前修复以禁用自动格式化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-30
      • 2013-05-26
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多