Forums

Remove <p> tag from excerpt (9 posts)

  1. neufuture
    Member
    Posted 2 years ago #

    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;
      ?>
  2. monkeychubs
    Member
    Posted 2 years ago #

    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.

  3. nims
    Member
    Posted 2 years ago #

    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.

  4. xiatica
    Member
    Posted 2 years ago #

    Nice one, thanks.

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

  5. rperezmicheli
    Member
    Posted 2 years ago #

    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!

  6. richard.calahan
    Member
    Posted 2 years ago #

    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.

  7. jezThomp
    Member
    Posted 2 years ago #

    Thanks Richard, thats awesome.

  8. pitarr
    Member
    Posted 1 year ago #

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

  9. richard.calahan
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic