• Resolved mcyzyk

    (@mcyzyk)


    Are there any working examples depicting how to use this via the do_shortcode() function in a template? This does not work:

    echo do_shortcode( '[GDC_row]' );

    // Start The Loop

    if ( $the_query->have_posts() ) :

    while ( $the_query->have_posts() ) : $the_query->the_post();

    echo do_shortcode( '[GDC_column size="third"]' );

    echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2><p>';

    get_template_part( '/template-parts/content-fulldisplay' );

    echo do_shortcode( '[/GDC_column]' );

    endwhile;

    endif;

    echo do_shortcode( '[/GDC_row]' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter mcyzyk

    (@mcyzyk)

    Ok, Claude clued me in. Looks like you need to build a big string of your content first, then feed it to the do_shortcode() function for interpretation/execution all at once:

    // Build the entire shortcode string first

    $output = '[GDC_row]';

    // Start The Loop

    if ( $the_query->have_posts() ) :

    while ( $the_query->have_posts() ) : $the_query->the_post();

    $output .= '[GDC_column size="third"]';

    $output .= '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';

    // Capture the template part output

    ob_start();

    get_template_part( '/template-parts/content-fulldisplay' );

    $output .= ob_get_clean();

    $output .= '[/GDC_column]';

    endwhile;

    endif;

    $output .= '[/GDC_row]';

    // Process all shortcodes at once

    echo do_shortcode( $output );

    // Reset Post Data to restore the original query (important if you have a main loop elsewhere on the page)

    wp_reset_postdata();

    This works, sort of. It gives me a single row with 1 column that’s a third of the screen. I want a single row with 3 columns.

    • This reply was modified 3 months, 1 week ago by mcyzyk.
    Thread Starter mcyzyk

    (@mcyzyk)

    Well OK! The issue was my Row tags needed to be outside my foreach loop:

    $output = '[GDC_row]';

    foreach ($selected_comparisonIDs as $thisComparisonID) {

    // Define the specific post ID you want to display

    $specific_post_id = $thisComparisonID; // Replace 26 with your actual post ID

    // Arguments for WP_Query

    $args = array(

    'p' => $specific_post_id, // Query the specific post ID

    'post_type' => 'any', // Set post type to 'any' to ensure it finds pages, custom post types, etc.

    );

    // Create a new WP_Query instance

    $the_query = new WP_Query( $args );

    // Start The Loop

    if ( $the_query->have_posts() ) :

    while ( $the_query->have_posts() ) : $the_query->the_post();

    $output .= '[GDC_column size="third"]';

    $output .= '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';

    // Capture the template part output

    ob_start();

    get_template_part( '/template-parts/content-fulldisplay' );

    $output .= ob_get_clean();

    $output .= '[/GDC_column]';

    endwhile;

    endif;

    // Reset Post Data to restore the original query (important if you have a main loop elsewhere on the page)

    wp_reset_postdata();

    }

    $output .= '[/GDC_row]';

    // Process all shortcodes at once

    echo do_shortcode( $output );
Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.