When I’m in “the loop” (archive pages etc.) how do I force teasers / excerpts for all articles, regardless of whether they contain the <!--more--> tag or not?

Details:

I’m starting off of the _s template which displays blog post content using the content.php template in which it calls the_content() function. This function inspects the post and tries to find the <!--more--> tag in. If it finds one, it returns a teaser and a read more link, if it doesn’t it simply outputs the whole post.

I’d like to keep the first part, i.e. respect the <!--more--> tag if the content author used it, but if he/she forgot it I want to automatically display the teaser / excerpt anyway (something like the first paragraph or two).

What is the best way to do this? There is a the_content filter but e.g. the read more customization text doesn’t come into it. What would you recommend?

2 s
2

EDIT

OK, there is a very hidden native function, get_extended() that I never knew about and greatly explained by @kaiser in his answer. My answer should only be an extension to the answer by @kaiser

ORIGINAL ANSWER

There is no native function to do this. The best here would be to use a PHP function like strpos to search for the more tag, and then do something according to the result.

You can try something like this

if( strpos( $post->post_content, '<!--more-->' ) ) {
    the_content( __( '&hellip; Read more about this article <span class="meta-nav">&rarr;</span>' ) );
}
else {
    the_excerpt();
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *