Site logo

Archived topic

Snippet inside query loop - number field (with logic)

5 replies · Started by Jason on November 2, 2022

Viewing posts 1–6 of 6

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.

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.

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.

You're welcome Jason!

Marked as resolved. Thanks, again

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

This archived topic is closed to new replies.