Word limit in post_content after more 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.

Leave a Comment