WordPress is one of the most popular content management system and blogging platforms in the world. A rough estimate says that about 20% websites on the internet are running on WordPress.
Sometimes WordPress developers face an odd problem of the post excerpt text coming closed inside a P (paragraph) tag. WordPress provides the_excerpt(); function to display excerpt of a post or a page. But when developers use this function, they notice that the output text is automatically being wrapped inside a <p> tag. In most cases this does not create any problem but in some instances where you're trying to do a specific thing with the excerpt text —this unwanted extra paragraph tag turns out to be a nuisance.
Not only the excerpt but the output of category description and the_content(); functions also come wrapped the same way. Let us see how to get remove P tag from output content.
WordPress applies certain filters to outputs of various functions. It is one of these filters that applies p tag around the excerpt text.
This is done probably to proactively help WP developers and display the excerpt text in a neat and clean way. But sometimes this pro-activeness of WP irritates the developers because wrapping a paragraph tag around excerpt text is easy but removing and existing tag requires efforts. Developers feel that WP should spit out raw text to enable developers to stylize it as they wish.
The best and cleanest way to get rid off this tag is to remove the filter that applies it.
Go to your functions.php file (Appearance > Editor > functions.php) and add the following code at the end of this file:
remove_filter ('the_exceprt', 'wpautop');
if you want to remove P tag from around content output, add this in the file:
remove_filter ('the_content', 'wpautop');
to remove P tag from category description, use:
There is also a way to get raw text without removing the filter. Instead of using the_except(); and the_content(); functions to get the text, you should use:
<?php echo get_the_excerpt(); ?>
<?php echo get_the_content(); ?>
these functions return plain and raw text without putting it through the filter mentioned above.
I hope this article helped you and save you some time. Please let me know if you have any question or comment! Thank you for using TechWelkin.