WordPress the_content (8)
|
Apr 28, 2006
|
I've never been a big fan of how WordPress treats my post content (the_content ). It automatically puts it within paragraph tags (<p> ), and there's no way I can disable that.
Or is there? I searched for it and found that other people are unhappy with this function. This WordPress support topic pointed to a plugin called WP Unformatted, written by WordPress guru Alex King. The plugin somewhat clumsily disables the auto-formatting when a custom field is added to a post. That's a lot of work, seeing that you'd have to add that custom field to every single post to make everything look the same. This guy found the function that applies this auto-formatting (which can also be found in the WP Unformatted plugin if you're a little bit smart): It's named "wpautop" and it's called in the "default-filters.php" file in the "wp-includes" directory. His suggestion is to edit the file by commenting out the call to that function.
I've never been a fan of editing core WordPress files because it adds a lot of confusion when upgrading. So the other obvious alternative is to make a little plugin or include in some "my-hacks.php" file this little bit of code: "remove_filter('the_content', 'wpautop') ". This does the trick, but it causes a few other problems. If a post has any type of formatting, such as paragraphs or line breaks, they're all gone. Disabling that filter simply outputs plain text, though the text still looks like it has formatting [1]. Another weird thing that happens is that the_content has a blank space before the first character of the first word. I'm not sure why this is happening, but I verified that it happens in more than one theme, which means it's not just a problem caused by my theme.
A WordPress support page said that the the_content() function simply applies filters to the get_the_content() function, meaning that a user could call "echo get_the_content() ", and it would output the stripped-down version of a post's content. I tried it and found that it works great and doesn't have that blank space at the beginning. Problem solved! Except that it creates a whole bunch of new problems, the most important of which being plugins. Most plugins apply filters to the_content , which means they would have no effect if the_content isn't called. So I'm back to where I started. WordPress puts the_content in paragraph tags. Suck it up.
[1] The plain text looks like it still has formatting (when viewing the source) because that's how it's stored in MySQL. It has line breaks and spaces, but HTML doesn't display it like that. The solution to that problem is to put it in <pre> tags and include word wrap with the "wrap='virtual'" attribute. You can also apply styling to <pre> tags like this: <pre wrap="virtual" style="font-family:trebuchet ms; font-size:10pt;"> #technology
|
Comments:
2006-05-07 15:49:12
Another thing I forgot to mention: When using get_the_content, posts and pages that used to display php code no longer work. For example, if one of your posts or pages executes a php script (by utilizing RunPHP or Exec-PHP), the code is simply outputted as text because these plugins apply filters to the_content without ever touching get_the_content.
2006-05-24 12:56:02
Jerome made a plugin similar to Alex King's, called WP-PreFormatted. It sounds like it stores a copy of each post, which seems like it would double the size of your database. I haven't tried it, but it was worth mentioning.
2007-03-18 18:23:43
[...] inserting double paragraph tags on all the posts. After a while I found an explanation saying to change a line in the main template. That fixed the immediate problem, and then I went through old posts, cleaning up some goofy syntax [...]
2007-04-06 21:36:54
I don't know if it helps, but if line breaks are your only worry, you can just use nl2br()-function around the_content() when you've disabled the wpautop-function. Like so: "echo nl2br(the_content());"
2007-04-09 14:07:51
Yeah I suppose that would work too. Thanks for the suggestion.
2008-01-15 13:36:02
SOLUTION WITHOUT HACKING ;)
Add a single line at the bottom of your themes CSS file:
p { margin: 0px; padding: 0px; padding-bottom:1.2em; }
and change padding-bottom: to whatever fits your needs ;)
Spread this! :)
2008-04-29 17:43:10
THANK YOU!!!!!
2010-09-26 20:17:12
Awesome! I used echo get_the_excerpt(); and it works wonders getting rid of the ! thanks so much
|