通过php functon从wordpress短代码中删除空的<p>标签

Jas*_*son 14 php wordpress function shortcode

寻找一个PHP函数(非jQuery或wpautop修改)方法<p></p>从wordpress中删除.

我试过这个,但它不起作用:

        function cleanup_shortcode_fix($content) {   
          $array = array (
            '<p>[' => '[', 
            ']</p>' => ']', 
            ']<br />' => ']',
            ']<br>' => ']'
          );
          $content = strtr($content, $array);
            return $content;
        }
        add_filter('the_content', 'cleanup_shortcode_fix');
Run Code Online (Sandbox Code Playgroud)

m1r*_*1r0 14

只需在您的functions.php文件中插入此代码:

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop', 99 );
add_filter( 'the_content', 'shortcode_unautop', 100 );
Run Code Online (Sandbox Code Playgroud)

  • 最好的解决方案我见过这个问题,欢呼! (3认同)

Gre*_*ham 5

add_filter('the_content', 'cleanup_shortcode_fix', 10);

我发现如果你指定10作为优先级它是有效的; 没有其他数字可行.


Nar*_*ryl 0

也许正则表达式可以工作:

$string=preg_replace_('/<p>\s*</p>/', '', $string);
Run Code Online (Sandbox Code Playgroud)

这应该将任何<p></p>内容替换为空,或者将其中的空格替换为空,从而删除它们。

将正则表达式应用于 HTML 代码时,最好\r\n先删除 HTML 的 ,因为它们会阻止正则表达式工作。