1

I want to display the 3 last post with this loop.

It works but I don't know why but the excerpt is always the same.

What did I do wrong?

 <?php
    $args = array( 'numberposts' => '3' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        ?>
        <div class="wpb_column vc_column_container vc_col-sm-4">
            <div class="wpb_wrapper">
                <div class="wpb_text_column wpb_content_element  wpb_animate_when_almost_visible wpb_left-to-right wpb_start_animation">
                    <div class="wpb_wrapper">
                        <p>
                            <a href="<?php echo get_permalink($recent["ID"]) ?>">
                                <?php echo get_the_post_thumbnail($recent['ID']); ?>
                            </a>
                        </p>
                        <h3><a href="<?php echo get_permalink($recent["ID"]) ?>"><?php echo $recent["post_title"] ?></a></h3>
                        <p><?php echo get_the_excerpt($recent["ID"]); ?></p>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }
    wp_reset_query();
    ?>
3
  • Is your recent ID definitely different each time?
    – Aravona
    May 11, 2017 at 9:58
  • Your code looks ok. Are you sure there is no plugin that tampers with the excerpt?
    – cjbj
    May 11, 2017 at 10:16
  • @Aravona Yes the id is different
    – Kevin
    May 11, 2017 at 12:00

1 Answer 1

1

get_the_excerpt function has issues using it with wp_get_recent_posts so instead of get_the_excerpt function use wp_trim_excerpt function in your code as displayed below.

<?php
    $args = array( 'numberposts' => '3' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
    ?>
    <div class="wpb_column vc_column_container vc_col-sm-4">
        <div class="wpb_wrapper">
            <div class="wpb_text_column wpb_content_element  wpb_animate_when_almost_visible wpb_left-to-right wpb_start_animation">
                <div class="wpb_wrapper">
                    <p>
                        <a href="<?php echo get_permalink($recent["ID"]) ?>">
                            <?php echo get_the_post_thumbnail($recent['ID']); ?>
                        </a>
                    </p>
                    <h3><a href="<?php echo get_permalink($recent["ID"]) ?>"><?php echo $recent["post_title"] ?></a></h3>
                    <p><?php echo wp_trim_words( $recent['post_content'], 50, '...' ); ?></p>
                </div>
            </div>
        </div>
    </div>
    <?php
}
wp_reset_query();
?>
4
  • @Vinond Dalvi Now I have different excerpt bur all the content of the post is displayed. The wp_trim_excerpt() function does not seem to work
    – Kevin
    May 11, 2017 at 12:15
  • You have to use the excerpt_length filter developer.wordpress.org/reference/hooks/excerpt_length to control length of excerpt. The same applies to control get_the_excerpt length so it is not specific to wp_trim_excerpt May 11, 2017 at 12:21
  • I have added a filter in my functions.php but nothing happened when I used $recent['post_content']... The filter works only with the_excerpt() but still, the same excerpt is displayed when I'm using that :(
    – Kevin
    May 11, 2017 at 13:07
  • I have changed the code a bit so could you please try using it? May 11, 2017 at 13:34

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.