WordPress automatically applies paragraph formatting to excerpts using the <p> tag, which can sometimes disrupt the desired layout or styling of your website. If you’re looking to have more control over how your excerpts are displayed, the following PHP code snippet allows you to remove WordPress’s default automatic paragraph tagging and gives you the option to reapply it selectively.
The Code
<?php
// Remove the automatic <p> tag wrapping from excerpts
remove_filter('the_excerpt', 'wpautop');
// Optionally, reapply the filter if needed
add_filter('the_excerpt', 'wpautop');
How It Works
By inserting this code into your theme’s functions.php file or a custom plugin, you can manage the automatic formatting of your excerpts. The remove_filter function call stops WordPress from automatically wrapping excerpt text in <p> tags. Following this, you can choose to add_filter to selectively reapply the automatic paragraph tagging where it suits your layout needs.
Usage Tips
- Selective Formatting: You might remove the filter before a specific block of code where you don’t want the
<p>tags and add it back immediately after, to maintain the default behavior elsewhere. - Theme Consistency: Use this tweak to ensure consistent styling across different parts of your theme where excerpts are displayed.
Benefits
- Layout Control: Gain precise control over how excerpts are formatted on your site.
- Styling Flexibility: Easily apply your own CSS styling to excerpts without the interference of unwanted
<p>tags. - Customization: Tailor the appearance of your excerpts to match the overall design of your website.
By adjusting the excerpt formatting behavior, you can enhance the readability and visual appeal of your WordPress site’s summaries and teasers.

Leave a Reply