How to obtain a reference to the_excerpt() from custom loop

I have a script in archive.php, which is outside the loop, that calls on the function get_blog_links() to list all posts that belong the the current category (my “blog” category).

I’m trying to do some trace testing inside the “get_blog_excerpt() function in order to write out either the post excerpt or (if no excerpt appears) the first 55 words of content (the_excerpt) for each post.

Read More

However, I’m unable to obtain a reference to the_excerpt in my function.

Any help much appreciated.

//Blog Listing
function get_blog_links(){
    $myposts = get_posts();
    echo '<div>'; 
    echo '<ul>'; foreach($myposts as $idx=>$post){ ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  // THE FUNCTION CALL IM WORKING ON...
            <?php echo get_blog_excerpt();?></li>
    <?php } 
    echo '</ul></div>';
}


function get_blog_excerpt(){
    // return get_the_excerpt(); WORKS
    // return the_permalink();  WORKS
    // return the_excerpt(); DOES NOT WORK?
    // return get_the_content(); DOES NOT WORK?
}

Related posts

Leave a Reply

1 comment

  1. I think you have to setup_postdata() with get_posts() to get things that rely on global variables to work. or explicitly pass the post id with the function.