How can I output the HTML of a query loop to a variable?
-
I’m trying to output the HTML of a query loop to a variable, but can’t get this to work. I.e., I want to save the example generated HTML
https://example.com/my-post/<br />My Post`
in the variable$theoutput
I want to use ob_start because my eventual loop will be complex and I don’t want to use
sprintf()
Any ideas why this doesn’t work and shows nothing in
$theoutput
?function onepost() {
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'posts_per_page' => 1,
);
ob_start();
$query = new WP_Query( $args );
while($query->have_posts()) : $query->the_post();
the_permalink();
echo '<br />';
the_title();
endwhile;wp_reset_postdata();
$theoutput = ob_get_contents();
ob_end_clean();
return $theoutput;
echo $theoutput;
}
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.