3

I am using the following code to hide the teaser and show content only after more tag in loop:

<?php
$after_more = explode(
    '<!--more-->', 
    $post->post_content
); 
if( $after_more[1] ) { 
    echo $after_more[1]; 
} else {
    echo $after_more[0]; 
}
?>

Is there anyway to show only first 50 words instead of entire post content? I want to hide teaser and show 50 words after tag.

3

Use wp_trim_words function to limit the content to a certain number of words and returns the trimmed text. Example use of wp_trim_words function.

<?php

    $content = get_the_content();
    $trimmed_content = wp_trim_words( $content, 50, NULL );
    echo $trimmed_content;

?>

So I added wp_trim_words function in your code to get 50 words after <!-- more -->.

<?php
    $after_more = explode( '<!--more-->', $post->post_content );

    if( $after_more[1] ) {
        $content = $after_more[1];
    } else {
        $content = get_the_content();
    }

    $trimmed_content = wp_trim_words( $content, 50, NULL );
    echo $trimmed_content;
?>

Edited to show 50 words from content if there is no <!--more--> in post content.

  • That's great. :) – Robert hue Feb 25 '15 at 7:32
  • We could also use the get_extended() core function to handle the more general case: <!--more(.*?)?-->. For example: <!--more But wait, there's more! -->. – birgire Feb 25 '15 at 9:23
  • @birgire never saw this function before. I will looking into it. :) Thanks – Robert hue Feb 25 '15 at 9:36
  • Hello Robert, wanted to know what if the post has no <!--more--> tag. I see it hides all content. – Skotlive Feb 26 '15 at 14:23
  • I mean to say, is there any way to use both the above codes as u wrote in taxonomy within a if else conditional tag: If the post has <!--more--> tag, go for second option, and if the post has no <!--more--> go for the first option – Skotlive Feb 26 '15 at 14:35
3

A) The <!--more--> comment:

Here's a one-liner:

echo wp_trim_words( strip_shortcodes( strip_tags( get_the_content( '', true ) ) ), 50 );

where we use the second argument of get_the_content() to hide the teaser above the <!--more--> part in the post content.

B) The <!--noteaser--> comment:

Note that there exists the <!--noteaser--> comment, that we could use instead to control the teaser display from the post content:

....
<!--more--><!--noteaser-->
...

In that case we would use:

echo wp_trim_words( strip_shortcodes( strip_tags( get_the_content( '', false ) ) ), 50 );

We could also apply additional filters to the above output if needed.

For this case we might also use:

echo wp_trim_excerpt();

and then control the output with the excerpt_length, excerpt_more, the_content, and wp_trim_excerpt filters.

An example with Star Trek Lorem Ipsum:

For case A above:

Before:

Exceeding reaction chamber thermal limit. 
We have begun power-supply calibration. 
<!--more-->
Force fields have been established on all turbo lifts and crawlways. 
Computer, run a level-two diagnostic on warp-drive systems. 
Antimatter containment positive. 
Warp drive within normal parameters. 
I read an ion trail characteristic of a freighter escape pod. 
The bomb had a molecular-decay detonator. 
Detecting some unusual fluctuations in subspace frequencies.
Sensors indicate no shuttle or other ships in this sector. 
According to coordinates, we have travelled 7,000 light years 
and are located near the system J-25. 
Tractor beam released, sir. 
Force field maintaining our hull integrity. 

After:

Force fields have been established on all turbo lifts and crawlways. 
Computer, run a level-two diagnostic on warp-drive systems. 
Antimatter containment positive. 
Warp drive within normal parameters. 
I read an ion trail characteristic of a freighter escape pod. 
The bomb had a molecular-decay detonator. 
Detecting some unusual fluctuations in subspace...

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.