Domanda

when i use text editor in WP, i have two options :

  • or i stop adding automatic <p> and <br> tags,
  • or the opposit, it gives those tags automatically.

I get crazy by that like other millions of people on the web... i would like to work as on a normal html page : when i push "enter", it gives an empty line as we see it on the screen and when we add a code, it gives a the code which behaves like a code.

Now, for example, if i do this in the text :

"this is a sample sentences <h2>here i want two words with h2 style</h2> here my sentence continues...", in the reality it gives a result like this :

<p>this is a sample sentences <br />
<h2>here i want two words with h2 style</h2><br />
here my sentence continues...</p>

I understood and i tried to remove automatisation with adding remove_filter ('the_content', 'wpautop'); to the functions file but in this case at each line break i must add a code which is crazy.

Is there a solution ???

È stato utile?

Soluzione

remove_filter('the_content','wpautop');

//decide when you want to apply the auto paragraph    
add_filter('the_content','my_custom_formatting_function');

function my_custom_formatting_function($content){
if(get_post_type()=='YOUR_POSTYPE_NAME') //if it does not work, you may want to pass the current post object to get_post_type
    return $content;//no autop
else
    return wpautop($content);
}

Would you please add above code in your functions.php. I hope this is helpful for you.

Altri suggerimenti

First of all: please don’t use headings for styling! The purpose of headings is to structure your content, not to emphasize parts of it. For that purpose you can use bold, italics, font size or create additional styles. But not headings! (BTW, headings are also parsed by search engines).

Now about your problem: I'm not sure about this, but I think there might be two steps involved: first the heading tag <h2> will be broken into a new line (like true headings should be formatted), then the wpautop filter will replace the new line characters with <br/>. So the root of the problem is not the wpautop filter itself, you can keep that switched on, but the way heading tags are handled. If the heading would stay inline, the wpautop would not add <br/> tags. Maybe this answer could help with that? But it's just a guess.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top