Excerpt not showing on “Latest posts” area. Why?

I’m working on a wordpress theme that has a “latest post”-like area under the main content on the main page. The custom theme is based on the Adamos theme. So far, I’m using this after the main content is printed:

<?php

    wp_reset_query();

    $args = array( 'numberposts' => '2' );

    $recent_posts = wp_get_recent_posts($args);

    foreach( $recent_posts as $recent ){

        setup_postdata($recent);

        ?>

        <div class="index_recent_post">

            <div class="index_recent_title">

                <h3><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a>'; ?></h3>

            </div>

            <?php

                if ( has_post_thumbnail($recent["ID"]) ) {

                    echo get_the_post_thumbnail($recent["ID"], 'frontpage-thumbnail');

                }

                echo '<p>' . get_the_excerpt($recent["ID"]) . '</p>';


                echo '<a href="' . get_permalink($recent["ID"]) . '" class="recent_link">MORE</a>';

            ?>

        </div>

        <?php

    }

    wp_reset_query();

    ?>

However, while every single item for the latest posts (Title, thumbnail, link) works perfectly, the excerpt does not: It shows empty. If I remove the wp_reset_query() and setup_postdata() lines, it shows the excerpt from the MAIN post, but there doesn’t seem to be any way to get it to show the excerpt from the latest ones, even when the rest of the information for the latest posts is shown perfectly.

Read More

The excerpt, also, doesn’t show regardless of whether the post has a custom excerpt or not, so the issue isn’t with a function looking for a custom excerpt and not finding it. I can get a custom excerpt via $recent["post_excerpt"], but it will only get it if it’s a custom one – it won’t build one based on the content if a custom one doesn’t exist, which is less than ideal.

Has anyone dealt with this issue, and can you help me find what the problem might be?

Related posts

1 comment

  1. can you use the following codes.I tested this code in my local server.It is working properly.

    wp_reset_query();
    
    $args = array( 'numberposts' => '2' );
    
    $recent_posts = wp_get_recent_posts($args);
    
    foreach( $recent_posts as $recent ){
    
        setup_postdata($recent);
    
        ?>
    
        <div class="index_recent_post">
    
            <div class="index_recent_title">
    
                <h3><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a>'; ?></h3>
    
            </div>
    
            <?php
    
                if ( has_post_thumbnail($recent["ID"]) ) {
    
                    echo get_the_post_thumbnail($recent["ID"], 'frontpage-thumbnail');
    
                }
    
                //echo '<p>' . get_the_excerpt($recent["ID"]) . '</p>';
                $content= $recent["post_content"];
    
                $excerpt = wp_trim_words( $content, $num_words = 55, $more = null ); 
                echo $excerpt;
    
    
                echo '<a href="' . get_permalink($recent["ID"]) . '" class="recent_link">MORE</a>';
    
            ?>
    
        </div>
    
        <?php
    
    }
    
    wp_reset_query();
    

Comments are closed.