• Resolved bluedogranch

    (@bluedogranch)


    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)
  • The issue you’re facing is likely due to the behavior of the_permalink() and the_title(). Both functions output directly to the page rather than returning values that can be captured into a variable, which can conflict with ob_start() and prevent capturing the HTML correctly.

    To solve this, you need to use the functions that return values (e.g., get_permalink() and get_the_title()) instead of the ones that echo output directly (e.g., the_permalink() and the_title()).

    Here’s a corrected version of your function:

    function onepost() {

    // Set up the query arguments
    $args = array(
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'DESC',
    'post_type' => 'post',
    'posts_per_page' => 1,
    );

    // Start output buffering
    ob_start();

    // Run the query
    $query = new WP_Query( $args );

    // Check if the query has posts
    if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
    // Capture the post permalink and title
    echo '<a href="' . get_permalink() . '">' . get_permalink() . '</a><br />';
    echo get_the_title(); // Output the title of the post
    endwhile;
    endif;

    // Get the contents of the buffer
    $theoutput = ob_get_contents();

    // Clean (flush) the buffer and turn it off
    ob_end_clean();

    // Return the captured HTML content
    return $theoutput;
    }

    Let me know if this works or if you need further clarification!

    Thread Starter bluedogranch

    (@bluedogranch)

    Thanks for the example, but it doesn’t work; echo $theoutput; shows nothing and var_dump($theoutput); is null.

    Thread Starter bluedogranch

    (@bluedogranch)

    This works:

    $args = array(
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'DESC',
    'post_type' => 'post',
    'posts_per_page' => 1,
    );
    $query = new WP_Query( $args );

    ob_start();

    while($query->have_posts()) : $query->the_post();
    the_permalink(); echo'<br />';
    the_title();
    endwhile; wp_reset_postdata();

    $out = ob_get_clean();

    echo $out;
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.