I am trying to find out how I can control how an excerpt is displayed using PHP. How do I get it to render the html spacing within the post instead of just showing one big paragraph.Hope that makes sense.

Let me give you an example.

So my client has syndicated post and at the top of each post he wants to include a hyperlink to the actual author . Then there needs to be a line break then the actual excerpt of the content under it. Currently the excerpt just shows everything as one paragraph.

I have managed to enter some php code to render the hyperlink in the excerpt but stuck on how to not let the excerpt strip the spacing.

  • If you want to output post content as-is, but just cut at a certain point, probably the use of the_content in combination with <!--more--> quicktag is best choice. See codex.wordpress.org/Function_Reference/the_content
    – gmazzap
    Mar 28, 2014 at 11:23
  • I don't want it to cut at a certain point, I just want the excerpt to not strip the html away. Mar 28, 2014 at 11:32
  • 1
    Are you talking about an excerpt manually inserted when publishing the post? Or an excerpt that is automatically generated from the full post content? In this 2nd case the excerpt is just the full content cut at a certain point... I was referring to that.
    – gmazzap
    Mar 28, 2014 at 11:43

2 Answers 2


The excerpt stips all html tags, a feature that frustrates many. I have written a custom excerpt that stops the excerpt from stipping any html tags. It also breaks the excerpt after the sentence after the set amount of words. If you need to cut the excerpt at exact words, you will just need to modify my code.

You will first need to remove the original excerpt, and then register the new excerpt. To do this, add the following code in your functions.php

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt'); 

Now for the custom excerpt. Add this below the code above

function pietergoosen_custom_wp_trim_excerpt($pietergoosen_excerpt) {
    global $post;
    $raw_excerpt = $pietergoosen_excerpt;
        if ( '' == $pietergoosen_excerpt ) {

            $pietergoosen_excerpt = get_the_content('');
            $pietergoosen_excerpt = strip_shortcodes( $pietergoosen_excerpt );
            $pietergoosen_excerpt = apply_filters('the_content', $pietergoosen_excerpt);
            $pietergoosen_excerpt = str_replace(']]>', ']]&gt;', $pietergoosen_excerpt);

            //Set the excerpt word count and only break after sentence is complete.
                $excerpt_word_count = 75;
                $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 
                $tokens = array();
                $excerptOutput = '';
                $count = 0;

                // Divide the string into tokens; HTML tags, or words, followed by any whitespace
                preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $pietergoosen_excerpt, $tokens);

                foreach ($tokens[0] as $token) { 

                    if ($count >= $excerpt_word_count && preg_match('/[\?\.\!]\s*$/uS', $token)) { 
                    // Limit reached, continue until ? . or ! occur at the end
                        $excerptOutput .= trim($token);

                    // Add words to complete sentence

                    // Append what's left of the token
                    $excerptOutput .= $token;

            $pietergoosen_excerpt = trim(force_balance_tags($excerptOutput));

                $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'pietergoosen' ), get_the_title()) . '</a>'; 
                $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 

                $pos = strrpos($pietergoosen_excerpt, '</');
                if ($pos !== false)
                // Inside last HTML tag
                $pietergoosen_excerpt = substr_replace($pietergoosen_excerpt, $excerpt_end, $pos, 0);
                // After the content
                $pietergoosen_excerpt .= $excerpt_end;

            return $pietergoosen_excerpt;   

        return apply_filters('pietergoosen_custom_wp_trim_excerpt', $pietergoosen_excerpt, $raw_excerpt);

If you need just specific tags, you can use the code below. Just add the code below, and the delete the tags that you need stripped.

function pietergoosen_get_allowedtags() {
// Add custom tags to this string that will be used as allowed tags
    return '<head>,<title>,<base>,<link>,<meta>,<style>,<script>,<noscript>,<body>,<section>,<nav>,
    <object>,<param>,<video> ,<audio>,<source>,<track>,<canvas>,<map>,<area>,<svg>,<math>,<table>,

Just add this line $pietergoosen_excerpt = strip_tags($pietergoosen_excerpt, pietergoosen_get_allowedtags()); below this line $pietergoosen_excerpt = str_replace(']]>', ']]&gt;', $pietergoosen_excerpt);

Hope this will help you

  • Thanks Pieter I will give it a go and see what happens. Will update you. Do I have permission to use this on all my clients sites if it works for me? Mar 28, 2014 at 11:57
  • All code given on WPSE is open to use and modify as you wish. So go ahead and use it as you wish Mar 28, 2014 at 11:59
  • My pleasure, glad that helped Mar 28, 2014 at 12:26
  • Sorry to ask another question but is there a way that I can strip images from this? Mar 28, 2014 at 13:57
  • Please see update Mar 28, 2014 at 14:26

The function wpautop automatically converts linebreaks to paragraph tags. Assuming your CSS-styling is set up correctly, you can apply wpautop to any text with newlines and WordPress will automatically add paragraph tags.

wpautop is automatically added as a filter to the_excerpt, so this should actually be doen by default. Could you post the code you're using to display the excerpt?

  • Thanks Engelen. Do I add this anywhere within the Functions.php file? sorry php is so not my strong suit. Mar 28, 2014 at 11:21
  • The excerpt stips html tags.... Mar 28, 2014 at 11:21
  • 1
    @DianeLaidlaw I've updated my answer; wpautop is automatically called on the_excerpt by default (if it's not removed by remove_filter). Please post the code you're using to display your excerpt.
    – engelen
    Mar 28, 2014 at 11:28
  • This is where it gets complicated. I am using a WordPress Framework that allows me to click and drag elements onto a canvas and it auto generates the code for me. All I need to do is select options of whether I want it to display excerpt or full post. Nevertheless I have created a child theme so I am assuming I will be able to have more control over how things are displayed if needed. Mar 28, 2014 at 11:32
  • If we don't know what function is used to display the excerpt we can hardly help you, unfortunately.
    – engelen
    Mar 28, 2014 at 11:37

Your Answer

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

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