Fetching and displaying data of a custom repeater attribute

Hey, I have a repeater field set up on my site but cannot get the data to display on the front end. The fields are displaying on my listing on the backend, the data is being saved to the database. Code is below, if anyone can let me know if I’m missing something? Thanks in advance.

add_filter( 'hivepress/v1/models/listing/attributes', function( $attributes ) {
    $attributes['vaccination_pricing'] = [
        'label' => 'Vaccine Prices',
        'editable' => true,
        'display_areas' => ['view_page_primary'],
        'edit_field' => [
            'type' => 'repeater',
            '_external' => true,
            'label' => 'Add Vaccine (Vaccine / Price)',
            'description' => '<div style="display: flex; justify-content: space-between; font-weight: bold;">
                                <span>Vaccine</span>
                                <span>Price</span>
                              </div>',
            'fields' => [
                'vaccine_name' => [
                    'type' => 'text',
                    'placeholder' => 'Enter Vaccine Name',
                ],
                'vaccine_price' => [
                    'type' => 'text',
                    'placeholder' => 'Enter Price',
                ],
            ],
        ],
    ];
    return $attributes;
}, 1000 );
add_filter( 'hivepress/v1/templates/listing_view_page/blocks', function( $blocks, $template ) {
    $listing = $template->get_context('listing');
    if ( !$listing ) {
        return $blocks;
    }

    $vaccination_pricing = $listing->get_attribute('vaccination_pricing');
    if ( !$vaccination_pricing ) {
        return $blocks;
    }

    return hivepress()->template->merge_blocks(
        $blocks,
        [
            'view_page_primary' => [
                'blocks' => [
                    [
                        'type' => 'content',
                        'content' => '<div class="hp-vaccination-pricing">
                            <p class="sidebar-heading"><span>Vaccine</span><span>Price</span></p>
                            <ul>' . implode('', array_map(function($item) {
                                return sprintf(
                                    '<li><strong>%s</strong>: %s</li>',
                                    esc_html($item['vaccine_name']),
                                    esc_html($item['vaccine_price'])
                                );
                            }, $vaccination_pricing)) . '</ul>
                        </div>',
                        '_order' => 20,
                    ],
                ],
            ],
        ]
    );
}, 1000, 2 );

Here is the template file for the Listing

<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"width":"66.66%"} -->
<div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:hivepress/listing-title /-->

<!-- wp:hivepress/listing-details-primary /-->

<!-- wp:hivepress/listing-images /-->

<!-- wp:hivepress/listing-description /-->

<!-- wp:hivepress/listing-attributes-secondary /--></div>
<!-- /wp:column -->

<!-- wp:column {"width":"33.33%","className":"flex-start"} -->
<div class="wp-block-column flex-start" style="flex-basis:33.33%"><!-- wp:hivepress/listing-attributes-ternary /-->

<!-- wp:hivepress/listing-attributes-primary /-->

<!-- wp:hivepress/listing-map /--></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

All I’m getting on the frontend

<div class="hp-listing__attribute hp-listing__attribute--vaccination-pricing">%value%</div>

Please try changing:

$listing->get_attribute('vaccination_pricing')

to:

$listing->get_vaccination_pricing()

This code should return an array of repeater data, then you can iterate over it and output HTML if needed.

Hope this helps

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.