[resolved] An excerpt of recent post outside the loop? (4 posts)

  1. estevancarlos
    Posted 6 years ago #

    How do I load an excerpt of the a recent blog post and use it outside the loop? I found a useful function but it requires specifying the ID of the post.


    function get_the_excerpt_here($post_id)
      global $wpdb;
      $query = "SELECT post_excerpt FROM wp_posts WHERE ID = " . $post_id . " LIMIT 1";
      $result = $wpdb->get_results($query, ARRAY_A);
      return $result[0]['post_excerpt'];

    I just want the most recent post.

    Is there a way I can use this for my specified needs? Or any other suggestions?

  2. Mark Ratledge
    Forum Moderator
    Posted 6 years ago #

  3. estevancarlos
    Posted 6 years ago #

    Actually I got it:

    <?php global $post; $myposts = get_posts('category_name=example'); ?>
    <?php if( isset( $myposts ) && $myposts != '' ) : foreach($myposts as $post) : setup_postdata($post);?>
    "><?php the_excerpt(); ?><?php endforeach; ?>
    <?php endif;?>

  4. chevas
    Posted 5 years ago #

    Estevancarlos: Dude seriously, you have no idea how helpful this is to me. Thank you. It looks like you found that same article by that Italian guy...maybe? I see the same function that he used in there and it looks like you successfully updated it with the modern "get_posts"

    Anyway, I don't write PhP, but I look at it enough to know a bit of what I need. This is so helpful, thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic