• I am using the Gutenberg Query Loop block to output a bunch of posts, and I want to be able to inject the post title and permalink (separately, with no wrapping markup) into custom HTML I have in the Post Template in the loop block. How can I do this?

    I can use shortcodes like [acf field=”blocks_to_campus”] to output ACFs, but I don’t see how I can do this with native post data. Trying to create a custom shortcode fails, since it tries pulling from the page/post the Query Loop is on, not the one being iterated through in the loop.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Not getting the currently iterated ID from the query loop block within the loop is a known problem for which there is unfortunately no generally valid solution to date. There is an issue on this at the Gutenberg team: https://github.com/WordPress/gutenberg/issues/43053

    There is also this very similar bug: https://github.com/WordPress/gutenberg/issues/40714

    Possible solutions can be found here:
    https://wordpress.stackexchange.com/questions/417823/get-the-id-retrieves-same-id-on-gutenbergs-query-loop (I currently usually go the first way described here via render_block)

    Thread Starter nitrospectide

    (@nitrospectide)

    I can make a simple shortcode, but how would I implement that render_block solution in my shortcode?

    There is an example linked in the last link above. This one here: https://github.com/5ally/xyz-block-demos/blob/trunk/class-xyz-shortcode.php

    Thread Starter nitrospectide

    (@nitrospectide)

    What I currently have is this in my functions.php

    /*
    ================================================================================
    == SHORTCODE: Output Link Buttons
    ================================================================================
    */

    function shrt_link_buttons( $atts ) {
    $theTitle = get_the_title();
    $theLink = get_the_permalink();
    $content = "";
    $content .= '<div class="btn-cta">
    <a class="btn" href="'. $theLink .'">
    See Details &gt;
    </a>
    <a class="btn" href="/get-property-showing?property='. $theTitle .'">
    See in Person &gt;
    </a>
    </div>';
    return $content;
    }
    add_shortcode( 'link_buttons', 'shrt_link_buttons');

    What it looks like that example you linked is doing, is targeting 3 block types, and running any shortcodes used in them through this to create the proper context for targeting the data retrieval. But the particulars of what it is doing to accomplish that are completely opaque to me. Do I need everything in that example? Does it need to be a class like the way it’s set up? Does that mean it needs to be a completely independent plugin to work properly? (No use in functions.php or combining with existing custom plugin that is just procedural functions?)

    Thread Starter nitrospectide

    (@nitrospectide)

    After some more digging, I was able to find a solution. I do wonder though if there is much of a performance hit due to this running on all core/html blocks?

    My shortcode call in an HTML block now looks like: [link_buttons post_id=”{{post_id}}”]

    And the function that replaces that {{post_id}} token with the actual post ID is:

    function query_loop_id_render($block_content, $block) {
    if ($block['blockName'] === 'core/html') {
    $block_content = str_replace("{{post_id}}", get_the_ID(), $block_content);
    }
    return $block_content;
    }
    add_filter('render_block', 'query_loop_id_render', 10, 2);

    And now my shortcode is updated to:

    /*
    ================================================================================
    == SHORTCODE: Output Link Buttons
    ================================================================================
    */

    function shrt_link_buttons( $atts ) {
    $theTitle = get_the_title($atts['post_id']);
    $theLink = get_the_permalink($atts['post_id']);
    $content = "";
    $content .= '<div class="btn-cta">
    <a class="btn" href="'. $theLink .'">
    See Details &gt;
    </a>
    <a class="btn" href="/get-property-showing?property='. $theTitle .'">
    See in Person &gt;
    </a>
    </div>';
    return $content;
    }
    add_shortcode( 'link_buttons', 'shrt_link_buttons');

    Credit to this comment: https://www.ads-software.com/support/topic/query-loop-posts-in-gutenberg/#post-17758832

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.