0

One of my posts has the following content:

This is a test post. Nothing more,
nothing less.
Three lines before the jump
<!--more-->
And a couple of more lines
after the jump.

Note: these were written as you see them, in the WP editor, using the HTML tab - so we have no hidden <br> or anything else going on in the post content.

Also: No Manual Excerpt was defined for this post.

If I use the_content(), I get this:

<p>This is a test post. Nothing more,<br /> 
nothing less.<br /> 
Three lines before the jump</p> 
<p><a href="...#more-1336" class="more-link" title="Continue Reading">&raquo;</a></p> 

If, however, I use echo get_the_excerpt(); I get this:

This is a test post. Nothing more, nothing less. Three lines before the jump

No <p>s, nor <br />s (heck, not even the [...] I was half-expecting!)

What I'd really like to achieve in my theme is (within The Loop, of course):

  1. If a manual excerpt exists: display that (and I know I'll have to tweak the [...])
  2. Else, display the content, respecting any <!--more--> you may find.

So, I was starting with this

$excerpt = get_the_excerpt();
if($excerpt=='') { //right, isset or empty may be better
    the_content();
} else {
    echo $excerpt;
}

But I'm getting the feeling there's a better way? Or is there a quick fix for the formatting issue mentioned above?

1 Answer 1

2

the_content() and the_excerpt(), unlike their 'get_' brothers will process the content through some functions, one of them is wpautop() that will add p tags for you. To get the same formatting for both excerpt and content:

if ( has_excerpt() ) {
  the_excerpt();
} else {
  the_content(__('Read more'));
}
1
  • Lovely! Thanks for the tip (I knew of wpautop() but didn't realize it did more than enclosing in <p>s
    – FOR
    Commented Mar 4, 2011 at 15:21

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.