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.
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.