Site logo

[Resolved] Snippet inside query loop – number field (with logic)

Home Forums Support [Resolved] Snippet inside query loop – number field (with logic)

Home Forums Support Snippet inside query loop – number field (with logic)

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2399188
    Jason

    Hey y’all. I seem to be getting stupid with query loop blocks. You came through last time with the image array, now I need to do something similar with a number ACF field, and I’m sure I’m just messing up the syntax.

    In a nutshell, I have 3 product price fields:
    pdf_price
    pod_price
    product_price

    For the archive and single pages, an if statement checking if the field has value and then displaying it WORKS (then using a shortcode with the portable hook shortcode method). This doesn’t work for the query loop, as I learned on the last go-round with the images. Here is the code you gave me to display images (set to array with logic) in a query loop:

    add_filter( 'render_block', function( $block_content, $block ) {
        
        $image = get_field('product_main_image');
    
        if ( !is_admin() 
            && $image
            && ! empty( $block['attrs']['className'] ) 
            && strpos( $block['attrs']['className'], 'my-query-header' ) !== false
        ) {
            $block_content = '<img class="product-main-image" src="' . esc_url($image['url']) . '" class="product-main-image" alt="' . esc_attr($image['alt']) . '"/>';
        }
    
        return $block_content;
    
    }, 10, 2 );

    Giving any block in the query loop the class of “my-query-header” displays the image beautifully. Now – how do I do the same thing with 3 different number fields? (if no data exists in a particular field, it shouldn’t display)

    Thanks, as always for your time.

    #2399234
    Fernando
    Customer Support

    Hi Jason,

    Have you tried directly getting it from the Dynamic settings of the Headline Block?

    Just set the source to Post Meta and the Post meta field to the custom field slug.

    By default, it shouldn’t display anything if the meta is null.

    Let us know.

    #2399241
    Jason

    Perfect – I just added three headline blocks and set them to their respective post meta names. It works! I was overthinking it, per usual. Thank you so much! You guys are awesome.

    #2399255
    Fernando
    Customer Support

    You’re welcome Jason!

    #2400844
    Jason

    Marked as resolved. Thanks, again

    #2400853
    Fernando
    Customer Support

    You’re welcome Jason! Glad to be of assistance!

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