$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>posts - get_the_excerpt() is not working as expected - returns wrong text|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - get_the_excerpt() is not working as expected - returns wrong text

matteradmin9PV0评论

In my template's 'footer.php', I'm attempting to generate a list of the four most recent blog posts, showing the featured image, title, excerpt, and a link to view.

The title, featured image, and URL are correctly generated, but the function get_the_excerpt($post_id) is returning unexpected results.

Sometimes, it returns the correct text excerpted from the blog post. Other times (i.e. upon refresh), it returns an excerpt from the first page of the site, not even a blog post.

Here's a screenshot:

Dumping the $rp variable shows the correct posts are returned. I'm using setup_postdata() per this page.

Am I using get_the_excerpt() incorrectly?

Note that the posts don't have manually-defined excerpts. I'm simply trying to get an auto-generated excerpt of each post.

Here's the code:

<!-- Recent posts -->
        <div class="recent-posts row">
            <?php 
            // Get list of recent posts:
            $rp = wp_get_recent_posts([
                'numberposts' => 4          
            ]);
            var_export($rp);

            foreach($rp as $p) : 
                global $post;
                $post = $p;
                setup_postdata($post);
                ?>
                <div class="col-sm-3">
                    <div class="card" width=>
                        <img class="card-img-top" src="<?=get_the_post_thumbnail_url( $p['ID']) ?>" alt="<?=$p['post_title'] ?>">
                        <div class="card-body">
                            <h5 class="card-title"><a href="<?=get_permalink( $p['ID'] ) ?>"><?=$p['post_title'] ?></a></h5>
                            <p class="card-text"><?=get_the_excerpt( $p['ID'] )?></p>
                            <a href="<?=get_permalink( $p['ID']) ?>">Read full post</a>
                        </div>
                    </div>
                </div>
            <?php
            endforeach;
            wp_reset_query();
            ?>
        </div> <!-- end recent-posts -->

UPDATE: So I got this to work, thanks to @SallyCJ in the comments below. Here's how:

When iterating through the posts returned by wp_get_recent_posts(), each post needs to be retrieved as an object and assigned to the global $post variable:

// (inside loop)
global $post;
$post = get_post($p['ID'], OBJECT);  // Retrieve the post as an object.
// NOW `get_the_excerpt()` works properly. 

NOTE that there appears to be a bug in get_the_excerpt, as it completely ignores any argument passed in. I can pass any post ID, and it always returns an excerpt for the current post.

Post a comment

comment list (0)

  1. No comments so far