Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

At times, I need to return the output of a loop (usually with WP_Query like in this example) for use in a shortcode or with a filter on the_content.

The following code that uses object buffering works, but I've read in other places that buffering can be inefficient. I've seen HEREDOC too, but I don't see how that would work here unless I saved every template tag as a variable first (which seems inefficient again).

So my question is, what's the best way to return the output of a loop?

<?php if ( $cms_pl_pages->have_posts() ) :
ob_start(); // start object buffering. we'll run the loop and spit out final contents.
echo '<section class="cms-pl-gallery">';
while ( $cms_pl_pages->have_posts() ) : $cms_pl_pages->the_post();
?>
    <article class="cms-pl-item clearfix">
        <?php has_post_thumbnail() ? the_post_thumbnail() : null; ?>
        <a href="<?php the_permalink(); ?>" title="Read '<?php the_title(); ?>.'">
            <h2><?php the_title(); ?></h2>
        </a>
        <?php has_excerpt() ? the_excerpt() : null; ?>
    </article>

<?php endwhile;
echo '</section> <!-- end .cms-pl-gallery -->';
$content = ob_get_contents(); // set $content to buffered object
ob_end_clean(); // throw away the buffered object
endif; wp_reset_postdata();
return $content; // return! ?>
share|improve this question
1  
I don't think output buffering is a significant performance issue. There is some extra overhead to store the content in memory, but it's not going to be noticeable for a typical post and its markup. Also, keep in mind that using a caching plugin makes any performance hit a non-issue. It's still important to think about efficiency, but it's more important to write maintainable code. So, output buffering is definitely preferable to HEREDOC comments or other ugly hacks. –  Ian Dunn Sep 13 '12 at 15:48
add comment

1 Answer 1

up vote 6 down vote accepted

There are replacements that return pure strings for all parts, no need to print anything into an output buffer. I like sprintf() and would write your example like this:

<?php
if ( $cms_pl_pages->have_posts() )
{
    $content = '<section class="cms-pl-gallery">';
    while ( $cms_pl_pages->have_posts() )
    {
        $cms_pl_pages->the_post();
        $content .= sprintf(
            '<article class="cms-pl-item clearfix">
                %1$s
                <h2>
                    <a href="%2$s" title="Read %3$s">%4$s</a>
                </h2>
                %5$s
            </article>',
            get_the_post_thumbnail(),
            apply_filters( 'the_permalink', get_permalink() ),
            the_title_attribute( array( 'echo' => FALSE ) ),
            the_title( '', '', FALSE ),
            has_excerpt() ? apply_filters( 'the_excerpt', get_the_excerpt() ) : ''
        );
    }
    $content .= '</section> <!-- end .cms-pl-gallery -->';
}
wp_reset_postdata();
return $content;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.