Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Query loop with empty (non-content) columns

  • Ehy there dear team,

    Do you think it’s possible to create a structure like this? (I’ve attached an example in the private info.)
    Basically, it’s a query loop with a grid layout, and every few columns there’s one column containing an image (so it doesn’t display any post content).

    I’ve tried experimenting a bit, but I can’t get it to work. Is this possible with the current query loop, or does it require custom code?

    Thankss

  • Hi there,

    So you want to insert content between the posts at random places?

    Let me know 🙂

  • Ehy Ying,

    exactly!

  • 1. Add this PHP code:

    add_filter( 'render_block', 'yh_render_loop_items', 10, 3 );
    
    function yh_render_loop_items( $block_content, $block, $instance ) {
    
        if ( 
            'generateblocks/loop-item' === $block['blockName'] && 
            ! is_admin() && 
            ! empty( $block['attrs']['className'] ) && 
            strpos( $block['attrs']['className'], 'my-loop-item' ) !== false 
        ) {
            $loop_index = $instance->context['generateblocks/loopIndex'] ?? 0;
    
            // Define your custom insertion points:
            $insertion_points = [
                2 => 'after_first_loop_item',
                4 => 'after_third_loop_item',
                11 => 'after_tenth_loop_item',
            ];
    
            // Check if current loop index matches one of them
            if ( array_key_exists( $loop_index, $insertion_points ) ) {
                ob_start();
                do_action( $insertion_points[ $loop_index ], $loop_index );
                $additional_content = ob_get_clean();
    
                // You can choose to prepend or append
                $block_content = $additional_content . $block_content;
            }
        }
    
        return $block_content;
    }

    2. Modify this section to suit your needs:

      $insertion_points = [
                2 => 'after_first_loop_item',
                4 => 'after_third_loop_item',
                11 => 'after_tenth_loop_item',
            ];

    3. Add my-loop-item class to the loop item block.

    4. You can use block element – hook to insert content, and set the hook name to after_first_loop_item/after_third_loop_item/after_tenth_loop_item to different places.

  • Hey Ying,

    I’m trying to test it. When I add the hook name, it doesn’t find those values you gave me as examples. Here’s a screenshot: https://imgur.com/uLqlklS

    So I can’t add them as hook names.

    Thanks

  • These are custom hooks you just created, so select custom hook, then enter the hook name.

  • Oh, I missed that I could add a custom hook, thanks!

    Anyway, unfortunately it doesn’t work. I checked the code with ChatGPT and it suggested adding this line at the beginning of the PHP code (before the function yh_render… part):

    add_filter( 'render_block', 'yh_render_loop_items', 10, 3 );

    This way, I see that it works. Does that make sense to you? I’m asking because I’m not very familiar with PHP and I’d rather have your confirmation than GPT’s :’D

  • Hi there,

    Yes, that’s correct — the function needs to be hooked to the render_block filter. It looks like Ying might have missed adding that part in her code. 🙂

  • Hi Alvind,

    great! Thanks guys,
    have a good day

  • No problem, you too! 🙂

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