Support » Themes and Templates » Remove paragraph tags from excerpt

  • In my template, on the “single post” page (single.php) I am displaying the excerpt with:
    <p class="intro"><?php the_excerpt(); ?></p>

    However my class is not getting applied because the excerpt function adds its own <p></p> tags to the excerpt.

    The docs say there are no parameters for this function. How can I remove the tags and just display the text?

Viewing 10 replies - 1 through 10 (of 10 total)
  • I’m not sure if that’s possible. However, I think you could get the same result by using a div instead.

    <div class="intro"><?php the_excerpt(); ?></div>

    Then in your CSS, use:

    .intro p{...}

    Hope that helps.

    Thread Starter svivian

    (@svivian)

    I thought about doing that, but I want to keep my code as clean as possible. I guess it’s the only solution for now.

    <?php category_description(); ?> has the same problem: <p> & </p> are added by default

    Try this….

    <?php remove_filter('the_excerpt', 'wpautop'); ?>

    Just removes the filter…

    <?php remove_filter(‘the_excerpt’, ‘wpautop’); ?>

    Mega man send his many thanks to you t31os. This really helped us.

    Glad i could help…. 😉

    Alternatively, yiu could just add the line

    remove_filter(‘the_excerpt’, ‘wpautop’);

    into your themes functions.php

    That would make it a global for any page/post/category within the theme…

    I’ve tried adding:

    remove_filter(‘the_excerpt’, ‘wpautop’);

    to the template and in my functions.php and neither has any effect. Am I missing something?

    I love the WordPress community!

    @arranp hmm… that’s strange, have you got it working now? This is how I have mine set up, and it case there is no excerpt, I call the_content_rss to print out a truncated version of the content, so it’s super clean.

    <?php #Display the excerpt if specified, otherwise use the content rss feed to display only the first 8 words (clever)
    	if (!empty($post->post_excerpt)) :
    		remove_filter('the_excerpt', 'wpautop');
    		the_excerpt();
    	else :
    		the_content_rss('', FALSE, '', 8);
    	endif;
    ?>

    robcleatongooglemailcom

    (@robcleatongooglemailcom)

    Thanks Rogerr!

    That’s excatly what I wanted, thank you very much!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Remove paragraph tags from excerpt’ is closed to new replies.