8

I've got a post format of Image, and I am running into an issue where the image is being wrapped by a <p> tag. I want to get rid of that tag (specifically on the single.php version) of those post types.

How can I get inside the formatting in a theme and remove the <p> tags, or create any format that I want for the output of this type of post, without affecting posts of a different post format?

3
  • 1
    Why does the <p> tag matter? My guess is that basic problem is just the styling. In that case, I imagine that a CSS solution is the easiest.
    – s_ha_dum
    Aug 8, 2013 at 22:31
  • the <p> is your friend. You should really learn how to use it. Aug 9, 2013 at 0:14
  • 5
    both of these comments are just bad. html matters for a variety of reasons, but in this case because the user is specifically asking for a solution to not use them.
    – albert
    Dec 18, 2018 at 16:41

12 Answers 12

4

By default, WordPress adds paragraph

tags to category descriptions. Stop this by adding the following to your functions.php file

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

Simple and easy (codeless).

Thank you

9

Wordpress automatically ads the <p> tags to the content. So it shows up while loading the content. This is with the filter wpautop. So we will remove this filter for the image post type only. You can manage this by adding the following code in functions.php file.

// Add the filter to manage the p tags
add_filter( 'the_content', 'wti_remove_autop_for_image', 0 );

function wti_remove_autop_for_image( $content )
{
     global $post;

     // Check for single page and image post type and remove
     if ( is_single() && $post->post_type == 'image' )
          remove_filter('the_content', 'wpautop');

     return $content;
}

is_single() checks if a single post is being displayed.

8

If this post type is called "image", you can create a single template to handle the display of just the image post type.

Just copy your 'single.php' file and rename the copy 'single-image.php'. Now you can control just the image posts. To strip out tags, I like to use the strip_tags() function. If you print the content of the post with the_content() it already applies the content filter, wrapping lines in <p> tags.

Here is an example of how you could get the content of your image without the tags:

$imageContent = get_the_content();
$stripped = strip_tags($imageContent, '<br><a>'); //replace <br> and <a> with whatever tags you want to keep after the strip
echo $stripped;

Hope this helps!

2

You can use get_the_content() instead of the_content(). This may solve your problem and another solution is same as described by @Chittaranjan

2

Simply add below line of code in your theme's functions.php file

For content :

remove_filter( 'the_content', 'wpautop' );

For excerpt

remove_filter( 'the_excerpt', 'wpautop' );

learn more : https://codex.wordpress.org/Function_Reference/wpautop

1

You can use specific post class for the single-post or single-format-standard and make it hide as you require only in a single page so that it won't be a conflict for other parts of the website.

Example CSS Code*

.single-post .entry-content p:empty { display: none; }

Example CSS Code for specific post format Image

.single-format-image .entry-content p:empty { display: none; }
1
  • Great suggestion! I didn't know :empty was a thing! Jul 10, 2020 at 17:06
0

Inorder to remove the p tag from the content you can use the below code

<?php remove_filter ('the_content', 'wpautop'); ?>
0

Another way to code it based on the solution by @chittaranjan

add_filter( 'the_content', 'remove_autop_for_image', 0 );

function remove_autop_for_image( $content ) {
     global $post;

     if ( is_singular('image'))
          remove_filter('the_content', 'wpautop');

     return $content;
}
0

put this code in "style.css" of "Active child theme"

p:empty {
  display: none;
}
1
  • 1
    +1 p:empty { display: none; } @Parthavi-Patel. It's good to make it hide with an empty element is found as removing the native functions is a bad practice though we have an option.
    – Maqk
    Jan 12, 2017 at 7:04
0

if want to remove from particular page or post you can call this

<?php remove_filter ('the_content', 'wpautop'); the_content(); ?>
-1

though the query has been answered, I'm posting the below for further reference.

remove_filter ('the_exceprt', 'wpautop');

remove_filter ('the_content', 'wpautop');

remove_filter('term_description','wpautop');

Source

-1

Using wp_strip_all_tags you can get rid of all html tags including p, div and etc https://developer.wordpress.org/reference/functions/wp_strip_all_tags/

wp_strip_all_tags(category_description());

1
  • The question is about stopping WordPress generating these tags automatically, not stripping tags from a database string.
    – Rup
    Dec 19, 2021 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.