7

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?

3

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();
}
  • Possibly the simplest approach. One downside is that an excerpt is actually quite different / constructed differently than what WordPress does for teasers so I'll need to create my own function for it and duplicate some of the internal WP code as it's not very well factored. But still, this is probably the easiest way to go about it, thanks. – Borek Bernard Dec 28 '14 at 16:17
  • If you need custom excerpts, you can have a look at this post – Pieter Goosen Dec 28 '14 at 16:20
8

Of course, there's an API function - hidden in the depth of core - to retrieve the parts before and after the <!--more--> tag in the content. Due to exactly zero naming conventions, the handy parts in WP are often overseen: get_extended().

// In the loop, after `the_post()` filled the global `$post`:
$info = get_extended( $post );

// The resulting data is an Array:
[Array] $info 
    'main'      => the text before the `<!--more-->`
    'extended'  => the content after the `<!--more-->` comment
    'more_text' => the custom "Read More" text

The result would look like:

if ( have_posts() )
{
    while ( have_posts() )
    {
        the_post();

        $info = get_extended( $GLOBALS['post'] );

        if ( ! empty( $info['extended'] ) )
        {
            the_content( ! empty( $info['more_text'] ) ? $info['more_text'] : 'Read more' );
        }
        else
        {
            the_excerpt();
        }
    }
}

and switch the excerpt and the content based on the existence of <!--more-->. Keep in mind that per default, WP only accepts <!--more--> without spaces. So if your authors type it by hand and add spaces, the <!-- more --> won't work (in any case).

  • Thanks for posting your answer. I never knew about this function. Great work and thanks for letting this hidden future seeing the light on WPSE – Pieter Goosen Dec 28 '14 at 16:40
  • @PieterGoosen Just figured out that there's the custom more_text returned as well. I wonder what that is. Maybe you want to do some research there? :) – kaiser Dec 28 '14 at 16:42
  • Well I'll probably have my own "mytheme_the_content()" function which will do the <!--more--> search and if it doesn't find it I'll get the rendered HTML content of the post and possibly just return the first two paragraphs from it. I don't like the output of the excerpts functions, and though there are ways to modify it as Pieter's post shows, it's probably too much hassle for my use case. Thanks for the great info. – Borek Bernard Dec 28 '14 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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