WordPress.org

Forums

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

  1. estevancarlos
    Member
    Posted 4 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.

    http://www.lonewolfdesigns.co.uk/excerpt-loop/

    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. estevancarlos
    Member
    Posted 4 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;?>

  3. chevas
    Member
    Posted 4 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