• It is annoying that the_excerpt() adds a <p> tag surrounding text or html, so I made a quick hack to remove them.:

    <?php
      $myExcerpt = get_the_excerpt();
      $tags = array("<p>", "</p>");
      $myExcerpt = str_replace($tags, "", $myExcerpt);
      echo $myExcerpt;
      ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • That’s brilliant – thank you very much!
    Does seem a bit odd that it comes with <p> tags by default, it’s much more use without them and so easy to add if required.

    Thanks for the hack. I also suggest that the_excerpt should come without <p> and it cud be added if reqd by developers in the theme.

    Nice one, thanks.

    I agree, p should be removed from the excerpt in general.

    rperezmicheli

    (@rperezmicheli)

    Nice. Just what I was looking for. Remember to tell folks to replace the excerpt call in index.php with this code. I commented out the old code and made a comment noting this new code and what it does. Thanks!

    You can technically just call

    <?php echo get_the_excerpt(); ?>

    instead of

    <?php the_excerpt(); ?>

    the first bit of code will not include paragraph tags. The function is looking for <p> tags that aren’t there.

    Thanks Richard, thats awesome.

    Where do i put this code to remove those annoying p tags???

    You simply call <?php echo get_the_excerpt(); ?> instead of <?php the_excerpt(); ?>

    The first tag is designed to retrieve the excerpt in plain text for manipulation in php functions, so if all you want to do is get rid of html tags, thats your answer. Just make sure you ‘echo’ or ‘print’ it, since by default it won’t render a display in your browser.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Remove <p> tag from excerpt’ is closed to new replies.