Site logo

Archived topic

Display YouTube videos using GB query loop block.

21 replies · Started by Maria on October 17, 2022

Viewing posts 1–15 of 22

Does GB Query Loop block work with YouTube videos? Unable to get it to work. I've created a CPT for Videos. Using ACF (oEmbed field) and a shortcode in GB Elements Content Template to show video on front end of the single page. Videos display properly. But when I create a Page to display all videos > insert query loop block with same settings as single page, the videos don't display on the front end. Just wondering if there's something else I need to show the videos using GB Query Loop block.

Hi Maria,

This is a core WordPress thing, the same issue will happen on WP's query loop block.

The shortcode added to a query loop block will not pull the ID of the current post, it will automatically fall back to the page ID.

One approach is to use the render_block filter to alter the shortcode block's content:
https://developer.wordpress.org/reference/functions/render_block/

So, if I use the render_block filter will the videos now show up on the page I've used the Query Loop block with? I'm not a developer so do I just copy and paste the filter as is? Where do I put it, in a Block Element hook?

No, you will need to write your own function first, then use the filter.

But I think using the GB headline block instead of the shortcode should be able to pull the post meta directly in the query loop.

Have you tried that yet?

That worked beautifully. Thank you so much.

Glad it works :)

You are welcome!

Hi

I'm also trying to make a loop with videos. I used the generateblock loop and it shows the post titles correctly but the video itself (created with CPT) I don't know how to include it. I tried calling the meta with the image block but it doesn't work.
I don't understand the way you describe here. Can you explain the steps to me?

Hi there,

are you using GenerateBlocks Pro ?

No

Ok - just an FYI the Pro versions supports simple ACF fields out of the box including Oembed.
Without it you need to use the render_block filter to inject the necessary code.
This doc shows the code we need from ACF:

https://www.advancedcustomfields.com/resources/oembed/

And this is the PHP Snippet to do that:


add_filter( 'render_block', function( $block_content, $block ) {
	
    if (
        !is_admin() 
        && ! empty( $block['attrs']['className'] )
        && 'acf-oembed' === $block['attrs']['className']
        ){
            $block_content = '<div class="embed-container">' . the_field('oembed') . '</div>';
    }
    
    return $block_content;

}, 10, 2 );

NOTES:

1. this line: && 'acf-oembed' === $block['attrs']['className'] - the acf-oembed is CSS Class ( change it to whatever you need ). This gets added to a Headline Block in your query loop. DO NOT set the dynamic data.

2. this: the_field('oembed') - change the oembed to the name of your field.

Haven't tested but it should work .....

It works great! Thank you very much!

Could an infinite scroll be added?

Infinite scroll will be a future option in GB Pro.

Glad to be of help :)

In the loop block I have to indicate how many posts to show per page but how do I change the page after? Is there a pagination?
I remember seeing it a while ago but now I can't find it.

If you select the Query Loop Block, theres an option in the block toolbar to insert the pagination.

I've looked everywhere but can't see it.

This archived topic is closed to new replies.