[Resolved] GB Query Loop – display ACF datas via shortcode

Home Forums Support [Resolved] GB Query Loop – display ACF datas via shortcode

Home Forums Support GB Query Loop – display ACF datas via shortcode

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #2302927
    akal

    Hi,

    I’m trying to display datas such as post cat (with acf featured img), post_tag, and other acf field value via a shortcode inside the GB Query Loop Block

    Here is my shortcode => https://snippi.com/s/0iltq8h

    This shortcode works when I use a custom template (WP Query) like here => https://g3n.camping-parcsaintjames.com/villages-vacances/parc-montana/#actus
    But when I put the shortocde inside the grid of the GB Query Loop Block it’s not working => https://g3n.camping-parcsaintjames.com/demo-gb-query-block/

    I’m wondering maybe it is not considered inside the loop so I have to rebuild the query in the shortcode ?

    Any help would be great ^^

    Best regards

    akal

    #2302943
    Fernando
    Customer Support

    Hi Gatineau,

    get_the_ID() doesn’t work in Query Loops whether it be the WordPress query loop block or the one from GenerateBlocks.

    See here for reference: https://github.com/WordPress/gutenberg/issues/35676

    Why not get the data through a Dynamic Headline/Image Blocks?

    #2303067
    akal

    Thanks for fast reply.

    I first wanted to use Dynamic Headline/Image Blocks but I can’t get the acf field attached to the current category post (acting like category featured image). When I choose Headline/Image Blocks and put the acf field within the dynamic option inside the Query Loop, nothing happens… see => https://g3n.camping-parcsaintjames.com/demo-gb-query-block/

    Also, I use ACF to display a numeric value (distance from the camping) like 10km. It’s ok to display the value, but the suffix ‘km’ I’ve set in the acf field doesn’t appear and I don’t know how to do it.

    That’s why I was wondering about building a shortcode… the fact is, I think there is no sense to rebuild a Query where the Query Block already does the job… so I’m stuck 😀

    #2303082
    akal

    Also, using GB Query loop elements (eg Headline block) I would like to display acf fields with several terms (like a multiselect or a checkbox) like here => https://g3n.camping-parcsaintjames.com/villages-vacances/parc-montana/hebergements/

    Reading this topic =>
    https://generatepress.com/forums/topic/display-a-custom-field-acf-which-has-several-terms-array/
    I was wondering about the GB Pro 1.3 update. Is ACF fully integrated now ? if yes, do you have documentation that explains how to configure GB & ACF (multiple terms, sufix, prefix, label, value , array… ) ?

    Reading Tom’s answer here => https://github.com/tomusborne/generateblocks/issues/457
    I’m not sure at this time I can achieve what I want by using the custom meta inside Dynamic Query…

    If BG doesn’t fully integrate acf yet, I definitively prefer the use of shortcode. But it appears I don’t know how to build shortcode that works inside GB Query Loop block, and so, if you have some doc it would be great, because it’s pretty new and I don’t find lot of doc at this time ^^

    #2303182
    David
    Staff
    Customer Support

    Hi there,

    at this time GB only supports the core post meta functions. Which does mean you can get simple field values from ACF, using the Content Source -> Post Meta in the GB Blocks Dynamic Data options.

    We are working on ACF integrations now. The next release will improve support for the standard ACF fields. And in future updates will be looking at more complex options like Repeater fields and options pages.

    Shortcodes in query loop blocks that rely on the Post ID are an issue today. There isn’t a good solution for using them inside a query loop.

    For today you may be best using your current solution for displaying those complex lists.

    #2303202
    akal

    Hi David, thanks for elaborating this answer.

    Ok so for now, I plan to pass all I need (taxonomy term, custom featured images and other complex field) into a shortcode fonction and then, add a shortcode block inside the GB Query Loop block.
    But, if a can’t use get_the_ID() in Query Loops, how can I retrieve custom data for each post inside the loop ? Should I create a new WP_Query inside de GB Query ? Or using foreach … when I try to display the current post (custom) values via shortcode, it returns all values from the loop on each post; do you have any idea ?

    https://g3n.camping-parcsaintjames.com/demo-gb-query-block/

    #2303759
    Fernando
    Customer Support

    You can use a Headline Block and an Image Block to retrieve terms and custom images respectively, given that they’re post meta.

    As for the complex data, the easiest route would be to filter the output of the Block you’re outputting. You can use render_block for instance.

    For instance, if you add class my-placeholder to a Block, and add this snippet:

    add_filter( 'render_block', function( $block_content, $block ) {
        if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-placeholder' ) !== false ) {
    		
            $block_content = '<h1>' . get_the_ID() . '</h1>';
    		
        }
    
        return $block_content;
    }, 10, 2 );

    This should return the IDs in the Block.

    get_the_ID() should now work through such a filter.

    #2304863
    akal

    render_block works like a charm, thank you !
    Could you please have a look on my code => https://snippi.com/s/75oqrcc
    Can I use render_block this way ?

    #2305636
    David
    Staff
    Customer Support

    That should be ok, i would suggest returning your HTML in $block_content variable.

    #2306412
    akal

    OK I’ll do that ^^
    Many thanks for your fast & efficient support guys !

    #2306534
    David
    Staff
    Customer Support

    Glad we could be of help

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