A working sample of a repeater?

Ideally on the vendor page, I would like to have a portfolio section, with
n-times the same thing :

  • a title (text)
  • a description

This can be done easily with ACF repeater (available in pro version).

I saw that you also have a repeater in HiverPress, but could not find so much documentation or examples about it.

Please help.

Hi,

You can try this sample code snippet:

add_filter(
	'hivepress/v1/models/listing/attributes',
	function( $attributes ) {
		$attributes['test'] = [
			'editable'   => true,

			'edit_field' => [
				'type'   => 'repeater',
				'label'  => 'Test',
				'_order' => 123,

				'fields' => [
					'title'       => [
						'type'        => 'text',
						'placeholder' => 'Title',
					],

					'description' => [
						'type'        => 'text',
						'placeholder' => 'Description',
					],
				],
			],

		];

		return $attributes;
	}
);

We also plan to make this field type available in the Attributes UI.

2 Likes

Hello there :slight_smile:

Great !

For those wonderring :
This does the same for the “vendors”

add_filter('hivepress/v1/models/vendor/attributes', function($attributes) {
    $attributes['portfolio'] = [
        'editable' => true,
        'edit_field' => [
            'type' => 'repeater',
            'label' => 'Portfolio',
            '_order' => 10,
            'fields' => [
                'title' => [
                    'type' => 'text',
                    'placeholder' => 'Project Title',
                ],
                'description' => [
                    'type' => 'textarea',
                    'placeholder' => 'Project Description',
                ],
            ],
        ],
    ];
    return $attributes;
});

But how do we render that in the listings or vendor profile so other users can actually see it ?

Thank you :slight_smile:

Simple : retrieve values and output them in a template with a shortcode.

What is your site ?

Hello cotasson :slight_smile:

I would have loved a more developed reply but that gave me a hint :slight_smile:

So for those who are wonderring not that just only how to add a repeater but as well how to display it in the front end here is a whole code snippet :
(this is for listings but this is the same process for vendor - code above)

add_filter(
	'hivepress/v1/models/listing/attributes', function( $attributes ) {
		$attributes['test'] = [
			'editable'   => true,
			'edit_field' => [
				'type'   => 'repeater',
				'label'  => 'Test',
				'_order' => 123,
				'fields' => [
					'title'       => [
						'type'        => 'text',
						'placeholder' => 'Title',
					],
					'description' => [
						'type'        => 'text',
						'placeholder' => 'Description',
					],
				],
			],
		];
		return $attributes;
	}
);

add_shortcode('listing_test', function($atts) {
    $listing_id = isset($atts['listing_id']) ? $atts['listing_id'] : get_the_ID();
    $listing = \HivePress\Models\Listing::query()->get_by_id($listing_id);
    
    if (!$listing) return '';
    
    $test_data = $listing->get_test();
    if (!$test_data) return '';
    
    $output = '<div class="listing-test">';
    foreach ($test_data as $item) {
        $output .= sprintf(
            '<div class="test-item"><h4>%s</h4><p>%s</p></div>',
            esc_html($item['title']),
            esc_html($item['description'])
        );
    }
    $output .= '</div>';
    
    return $output;
});

//Usage: [listing_test]

The add filter will add the custom repeater field here (Listings) :

You’ll also have it editable in the user/vendor/listing edit front end :

But so the other users will actually be able to see it in the front end listing “details” :

You need the “add_shortcode” from the code above
And you need to indeed setup a template for that.
And i personaly find it a real pain to identify which blocks we have to use to render at least something close to the “original” so anyway here is my setup :

I hope this will help whoever see that post :slight_smile:

1 Like

Salut Laurent,

Good job ! Thank you for sharing with the community.

I am sorry if I did not provide a more detailed explained, it was just 1 AM, and I was trying to get some sleep. But apparently you got my point.

:wink: