Conditional singular/plural label in Query Loop for custom field

  • Hi,

    I’m building a real estate website using GenerateBlocks and a Query Loop.
    I have a custom field called number_rooms created with Pods.

    Inside the loop, I can display the value using:
    {{post_meta key:number_rooms}}

    But I would like to display it like this:
    “1 room” or “2 rooms” — depending on the value.

    Is there a way to handle this conditional logic (singular/plural) directly inside GenerateBlocks or with a workaround?
    Maybe using a dynamic tag, a custom function, or something else?

    Thanks a lot for your help!

  • Hi there,

    Try replacing your current dynamic tag with {{post_meta key:number_rooms|room_tag}}.

    Then add this PHP code:

    add_filter( 'generateblocks_dynamic_tag_output', function( $output, $options ) {
        
        // Check if the custom dynamic tag is being used
        if ( empty( $options['room_tag'] ) ) {
            return $output;
        }
    
        // Get the ACF field value (current post context assumed)
        $rooms = get_field( 'number_of_rooms' );
    
        if ( $rooms ) {
            $output = $rooms . ' ' . ( $rooms == 1 ? 'room' : 'rooms' );
        } else {
            $output = ''; // Optional: clear output if no value
        }
    
        return $output;
    
    }, 10, 2 );

    Adding PHP: https://docs.generatepress.com/article/adding-php/

  • Thanks a lot for your help, it works perfectly!
    I really appreciate your support, this dynamic tag filter approach is exactly what I needed.
    It’s clean, flexible and perfectly suited for what I’m building with GenerateBlocks.

  • Glad to hear that 🙂

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