treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Wordpress Display Latest Posts' Excerpts

  • Hi all, I know this isn't a CSS thing, but after a few tries of posting my question in WP forums I thought I'd give this a try as I'm under a tight deadline.

    I'm trying to display the latest post's excerpts...but all I keep getting is the current page this code is on, as well as the thumbnail and post title. I can't seem to get the excerpt to work. Does anyone know a solution for this?

            <?php
              $args = array( 'numberposts' => '2', 'offset' => 1 );
              $recent_posts = wp_get_recent_posts( $args );
    
              foreach( $recent_posts as $recent ) {
    
        $excerpt = get_the_excerpt($recent['ID']);
    
              echo "
    
              <li>
              <div class='featured_image'>
              <a href='".get_permalink($recent['ID'])."'>".get_the_post_thumbnail( $recent['ID'], 'thumbnail' ). "</a>
              </div>
              <div class='post_content'>    
              <h2 class='post_title'><a href='".get_permalink($recent['ID'])."'>".$recent[post_title]."</a></h2>
    
              ". $excerpt ."
              </div>
    
    
    
              </li>"
    
              ;
              }
          ?>  
    

    Any help would be greatly appreciated.

  • Here's a link to the site I'm working on. The code is down at the bottom.

    http://www.robertallenbaker.com/dzindna/

  • Try using WP_Query

  • Code Pen will not run PHP, but here is a link to a none working example using your formatting. I have not verified this, so their might be a typo or two, but it will get you on the right track.

    Code Pen Link

    Check out the Wordpress Codex

  • Aaron, Thank you so very very very very much!! I always had a problem with loops, but after checking out WP_Query I noticed what you did there.

    It worked like a charm!

    Took a PHP class at Community College and I still can't get some of the basics down.

    Thank you thank you thank you!!