• I don’t really know php but I seem to manage at tweeking stuff till it works. I’m trying to do a shortcode that displays my most recent post and the thumbnail and excerpt. The thumbnail works fine, but can someone tell me why the excerpt isn’t working? it’s giving me the exceprt of the page that the shortcode is on, not of the recent post

    function my_recent_posts_shortcode($atts){
    // get the posts
        $posts = get_posts(
            array(
                'numberposts'   => 1
            )
        );
        // No posts? run away!
        if( empty( $posts ) ) return '';
        /**
         * Loop through each post, getting what we need and appending it to
         * the variable we'll send out
         */
        $out = '';
        foreach( $posts as $post )
        {
    $out .= sprintf(
            '<img class="frontpage" src="%s" title="%s"><h2>%s</h2>',
    	wp_get_attachment_url( get_post_thumbnail_id($post->ID) ),
            esc_attr( $post->post_title ),
    	the_excerpt()
            );
        }
        $out .= '';
        return $out;
    }

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Shortcode to display thumbnail and excerpt’ is closed to new replies.