首页 文章

删除WordPress帖子中的<p>和<br/>标签

提问于
浏览
45

每当我在WordPress帖子页面发布一些内容时,它会显示一些段落标签,如 <p>
. 这显示了输出中的一些额外空间 . 那有什么解决方案吗?如何删除所有标签?

9 回答

  • 2

    由于这里接受的答案在WP 4.8.1中对我不起作用,我发布了一个对我有用的解决方法 . 我只需要在p中添加一个类,WordPress就不会删除它 .

    <p class="whatever">
    
  • 0

    这是因为WordPress的 wpautop . 只需在主题的functions.php文件中添加以下代码行

    remove_filter( 'the_content', 'wpautop' );

    remove_filter( 'the_excerpt', 'wpautop' );

    有关更多信息:http://codex.wordpress.org/Function_Reference/wpautop

  • 95

    默认情况下,WordPress将段落 <p></p> 标签添加到类别说明中 . 通过将以下内容添加到 functions.php 文件来停止此操作

    // Remove p tags from category description
    remove_filter('term_description','wpautop');
    

    Simple and easy (codeless).

    谢谢

  • 0

    我不建议使用remove_filter选项,因为它会删除您在模板中调用它们的所有位置的标记和标记 .

    最好的方法是通过PHP清理你的字符串

    你可以使用php函数strip_tags删除无用的标记: echo strip_tags(get_the_excerpt()); // Get the raw text excerpt from the current post

    要么

    echo strip_tags(category_description()); // Get the raw text cat desc from the current cat page

    这将在调用时删除当前wordpress帖子中的所有HTML标记 .

    您还可以在以下情况下添加修剪功能: echo trim(strip_tags(get_the_excerpt())); // Get the raw text excerpt from the current post

    要么

    echo trim(strip_tags(category_description())); // Get the raw text cat desc from the current cat page

    这非常适合获取meta =“description”的原始文本和不建议使用HTML标记的 Headers .

    希望能帮助到你

  • 1

    在编辑器初始化之前使用此代码删除 <p></p> .

    function tinymce_remove_root_block_tag( $init ) {
        $init['forced_root_block'] = false; 
        return $init;
    }
    add_filter( 'tiny_mce_before_init', 'tinymce_remove_root_block_tag' );
    
  • 0

    试试这个

    $my_postid = $post->ID;
    $content_post = get_post($my_postid);
    $content = $content_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = strip_tags($content, '<p>
    '); echo $content;
  • 1

    有时在主题的functions.php中删除WordPress的wpautop函数不起作用(由于某些原因) .

    所以我有另一个解决方案如何停止删除 <br> 标签或双( <br><br> )换行符 .

    • 对您的文件进行更改 /wp-content/themes/your_theme_name/functions.php

    在函数顶部添加2行 .

    remove_filter('the_content', 'wpautop');
    remove_filter('the_excerpt', 'wpautop');
    

    这将首先关闭 wpautopop 功能 .

    • 在函数 wpautop 中对文件 /wp-includes/formatting.php 进行更改 .

    A)将 function wpautop( $pee, $br = true) 更改为 function wpautop( $pee, $br = false) .

    这将另外关闭所有地方的 wpautopop 功能 .

    B)将 $pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee); 更改为

    $pee1 = $pee;
    $pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee);
    $pee = $pee1;
    

    这将阻止系统删除双 <br> 标记 . (我知道代码很奇怪,但由于 ?> 标签,简单的 //$pee 在这里没有帮助) .

    C)将 $pee = preg_replace("/\n\n+/", "\n\n", $pee); 更改为 //$pee = preg_replace("/\n\n+/", "\n\n", $pee);

    这将阻止系统删除多个换行符 .

    D)改变这个:

    $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
    

    对此:

    //$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
    

    这将阻止系统在打开之后或关闭块元素标记之前删除换行符,如 <div><article> 等 .

    E)改变这个:

    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
    

    对此:

    //$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
    

    完全相同:这将阻止系统在打开之后或关闭块元素标记之前删除换行符,如 <div><article> 等 .

    F)改变这个:

    $pee = preg_replace('!
    (\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);

    对此:

    // $pee = preg_replace('!
    (\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);

    这将阻止系统在块结束时删除 <br> .

    G)改变这个:

    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*
    !', "$1", $pee);

    对此:

    //$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*
    !', "$1", $pee);

    这将阻止系统在打开或关闭块标记后删除 <br> .

    希望它会有所帮助!并阅读此文件中的评论 - 它们将帮助您了解打开或关闭所需的内容 .

  • 0

    删除 wpautop 过滤器不是最灵活的解决方案 . 如果您正在寻找逐页解决方案或邮寄帖子,您可以使用此插件:

    https://wordpress.org/plugins/dont-muck-my-markup/

    它将在每个帖子/页面上添加一个复选框,供您选择是否对该特定页面禁用core-Wordpress行为 .

    当您想要发布一些HTML代码并且自动生成的 <br><p> 搞砸了您精心设计的设计时,这尤其有用 .

    该插件还具有全局选项,因此您可以在所有页面/帖子上默认关闭此行为 .

    另一个插件是https://wordpress.org/plugins/preserve-code-formatting/但是我只尝试了我描述的插件 .

  • 0

    使用这个$ editor = wp_filter_nohtml_kses($ _ POST ['editor1']);

相关问题