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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6625458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:47:19  来源:igfitidea点击:

Removing <p> and <br/> tags in WordPress posts

wordpress

提问by NewUser

Whenever I am posting some content in WordPress post page it is showing some paragraph tags like <p>and <br/>. Which is showing some extra space in output. So is there any solution for it? How to remove all the tags?

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

回答by Santosh Sonarikar

This happens because of WordPress's wpautop. Simply add below line of code in your theme's functions.php file

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

remove_filter( 'the_content', 'wpautop' );

remove_filter( 'the_content', 'wpautop' );

remove_filter( 'the_excerpt', 'wpautop' );

remove_filter( 'the_excerpt', 'wpautop' );

For more information: http://codex.wordpress.org/Function_Reference/wpautop

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

回答by Gowri

try this

尝试这个

$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><br/>');
echo $content;

回答by Damien Sellier Dubois

I would not recommend using the remove_filter option since it will remove tags and markups everywhere you call them in your template.

我不建议使用 remove_filter 选项,因为它会删除您在模板中调用它们的任何地方的标签和标记。

Best way is to sanitize your string through PHP

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

you can use php function strip_tags to remove useless markups: echo strip_tags(get_the_excerpt()); // Get the raw text excerpt from the current post

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

or

或者

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

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

this will remove all HTML tags from the current wordpress post when calling it.

这将在调用它时从当前的 wordpress 帖子中删除所有 HTML 标签。

you can also add the trim function just in case of: echo trim(strip_tags(get_the_excerpt())); // Get the raw text excerpt from the current post

您还可以添加修剪功能,以防万一: echo trim(strip_tags(get_the_excerpt())); // Get the raw text excerpt from the current post

or

或者

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

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

this is perfect to get raw text for meta="description" and title where HTML markups are not recommanded.

这非常适合为不推荐使用 HTML 标记的 meta="description" 和标题获取原始文本。

Hope it helps

希望能帮助到你

回答by myvahid

Use this code to remove <p></p>before editor initialize.

使用此代码<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' );

回答by bg17aw

Removing the wpautopfilter is not the most flexible solution. If you are looking for a page by page solution, or post by post, you can use this plugin:

移除wpautop过滤器并不是最灵活的解决方案。如果您正在寻找逐页解决方案或逐页发布,您可以使用此插件:

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

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

It will add a checkbox on every post/page for you to choose if the core-Wordpress behavior will be disabled for that particular page or not.

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

This is especially useful when you want to post some HTML code and the auto generated <br>and <p>mess up your carefully engineered design.

当您想要发布一些 HTML 代码和自动生成<br><p>弄乱您精心设计的设计时,这尤其有用。

The plug-in also has a global option so you can have this behavior turned off by default on all pages/posts.

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

An alternative plug-in is https://wordpress.org/plugins/preserve-code-formatting/however I only tried the one I described.

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

回答by Alpesh Navadiya

use this $editor= wp_filter_nohtml_kses($_POST['editor1']);

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

回答by Pablo Santamaria

As the accepted answer here didn't work for me in WP 4.8.1, I'm posting a workaround that worked for me. I just had to add a class to the p and WordPress won't remove it.

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

<p class="whatever">

回答by Jodyshop

By default, WordPress adds paragraph <p></p>tags to category descriptions. Stop this by adding the following to your functions.phpfile

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

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

Simple and easy (codeless).

简单易行(无代码)。

Thank you

谢谢

回答by luan nguyen

in case you want to remove p tag in Wordpress post or textarea without a change in function, you can do a simple css-trick like : css :

如果您想在不改变功能的情况下删除 Wordpress 帖子或 textarea 中的 p 标签,您可以执行一个简单的 css-trick,如: css :

.container >p {
display:none;
}

html:

html:

<div class = 'container'>
<!the p tag u dont want here!>
</div>

回答by Kir Mazur

Sometimes removing WordPress's wpautop function in your theme's functions.php doesn't work (for some reasons).

有时在主题的functions.php 中删除WordPress 的wpautop 功能不起作用(出于某些原因)。

So I have another solution how to stop removing <br>tags or double (<br><br>) line breaks.

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

  1. Make changes to your file /wp-content/themes/your_theme_name/functions.php
  1. 更改您的文件 /wp-content/themes/your_theme_name/functions.php

Add 2 lines to the top of your functions.

在函数顶部添加 2 行。

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

This will initially turn off wpautopopfunction.

这将最初关闭wpautopop功能。

  1. Make changes in file /wp-includes/formatting.phpin function wpautop.

    A) Change function wpautop( $pee, $br = true)to function wpautop( $pee, $br = false).

    This will additionally turn off wpautopopfunction from all places.

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

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

    This will prevent the system from removing double <br>tags. (I know the code is strange but simple //$peedoesn't help here because of ?>tag).

    C) Change $pee = preg_replace("/\n\n+/", "\n\n", $pee);to //$pee = preg_replace("/\n\n+/", "\n\n", $pee);

    This will prevent the system from removing multiple line breaks.

    D) Change this:

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

    to that:

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

    This will prevent the system from removing line breaks after the opening or before the closing block element tag like <div>, <article>, etc.

    E) Change this:

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

    to that:

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

    Pretty the same: This will prevent the system from removing line breaks after the opening or before the closing block element tag like <div>, <article>, etc.

    F) Change this:

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

    to that:

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

    This will prevent the system from removing <br>at the end of the block.

    G) Change this:

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

    to that:

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

    This will prevent the system from removing <br>after an opening or closing block tag.

  1. /wp-includes/formatting.php在函数中更改文件wpautop

    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 . '[^>]*>)!', "", $pee);
    

    对此:

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

    这将防止系统从开口之后或闭合块元件标记之前等除去换行符<div><article>

    E) 改变这个:

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

    对此:

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

    漂亮相同的:这将防止系统从开口之后或闭合块元件标记之前等除去换行符<div><article>

    F) 改变这个:

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

    对此:

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

    这将防止系统<br>在块的末尾移除。

    G) 改变这个:

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

    对此:

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

    这将防止系统<br>在打开或关闭块标签后删除。

Hope it will help! And read comments in this file – they will help you to understand what you need to turn on or turn off.

希望它会有所帮助!并阅读此文件中的注释——它们将帮助您了解打开或关闭所需的内容。