1

I'm working on my own custom theme (first custom WP work so I'm a real beginner) and need help in listing the first 20 words of a list of recent posts.

I managed to solve this with manual? excerpt but I would like to just get the first 20 words of the content, but not sure how to best do that. The application is in a list on my start page.

My current code looks like this

    <?php
    $cdRP = get_theme_mod('cd_recent_posts', '3');
    $args = array( 'numberposts' => $cdRP );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        echo '<div class="grid-cell"><a class="fpItems" href="' . get_permalink($recent["ID"]) . '">';
        if ( has_post_thumbnail( $recent["ID"]) ) {
            echo  '<div>' . get_the_post_thumbnail($recent["ID"],'thumbnail') . '</div>';
        }
        echo '<div>'
        . '<h3>' . $recent["post_title"] . '</h3>'

        . '</div></a></div>';
    }
    wp_reset_query();
?>

All of this happens outside the loop. I tried to find the answer in the forums, but failed, so sorry if this has been asked and answered before. Trying my best to learn to code this wonderful tool my self :)

3
  • It is part of the process to select an answer as solution by giving it the green check mark. Feel free to chose your own. Commented Jan 5, 2018 at 16:52
  • oh, missed that. Thank you so much for pointing it out (blush)
    – Per
    Commented Jan 5, 2018 at 16:55
  • No worries, it is a common effort. Commented Jan 5, 2018 at 16:59

3 Answers 3

5

Use wp_trim_words()

wp_trim_words( get_the_content(), 20 )

As you're outside the main loop

wp_trim_words( $recent[ 'post_content' ], 20 )

If you want to apply the same filters as to the_content() in the main loop

wp_trim_words( apply_filters( 'the_content', $recent[ 'post_content' ] ), 20 )
6
  • Since you're outside The Loop, you might need to use wp_trim_words( get_the_content( $recent["ID"] ), 20 );.
    – Pat J
    Commented Jan 5, 2018 at 16:22
  • 1
    @PatJ get_the_content doesn't take the ID, but he has the post_content already available Commented Jan 5, 2018 at 16:23
  • 1
    Life savers. Thank you guys :) I managed to guess by just typing logical stuff that $recent["post_content"] got the content, but didn't manage to trim it. Hoped that $recent[post_excerpt"] worked, but no such luck ;) Thank your your help. both of you @PatJ and Nicolai
    – Per
    Commented Jan 5, 2018 at 16:31
  • @Per My pleasure and anytime. Commented Jan 5, 2018 at 16:33
  • 1
    @PatJ I already had written your exact suggestion, before I remembered the restriction. Commented Jan 5, 2018 at 16:37
2

Working Solution turned out to be simple :)

    <?php
    $cdRP = get_theme_mod('cd_recent_posts', '3');
    $args = array( 'numberposts' => $cdRP );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        echo '<div class="grid-cell"><a class="fpItems" href="' . get_permalink($recent["ID"]) . '">';
        if ( has_post_thumbnail( $recent["ID"]) ) {
            echo  '<div>' . get_the_post_thumbnail($recent["ID"],'thumbnail') . '</div>';
        }
        echo '<div>'
        . '<h3>' . $recent["post_title"] . '</h3>'

        . wp_trim_words( $recent["post_content"], 20 )

        . '</div></a></div>';
    }
    wp_reset_query();
?>
1

You can achieve this by using the following;

$content = get_the_content();
echo substr($content, 0, 20);

or

$content = get_the_excerpt();
echo substr($content, 0, 20);

This will limit the character count to 20.

1
  • 1
    That'll trim to 20 characters, not words, though.
    – Pat J
    Commented Jan 5, 2018 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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