Get listing attribute in the template hook

I have successfully added a snippet that adds a shortcode to the listing view page. The shortcode adds a property reviews from other platforms (AirBnB, Booking.com, etc…). I would like to make this shortcode different for each listing. I created a custom attribute for the listing called ‘review_short’ where I will store the shortcode for each property. How do I use this field in the listing.

Here is the snippet I used:

add_filter('hivepress/v1/templates/listing_view_page', function($template) {
	return hivepress()->helper->merge_trees($template,[
		'blocks' => [
			'page_content' => [
				'blocks' => [
					'custom short' => [
						'type' => 'content',
						'content' => do_shortcode('[trustindex data-widget-id=5a1f55c11154602b4d660eb6656]'),
						'_order' => 80,
					],
				]
			]
		]
	]);
});

Hi,

Please try this PHP snippet (you need to change the code get_your_attribute_field_name > get_ + slag attribute, you specified it in the Field name):

add_filter(
 'hivepress/v1/templates/listing_view_page/blocks', 
 function($blocks, $template) {
  $listing = $template->get_context('listing');
  
  if(!$listing){
   return $blocks;
  }
  
  $custom_attribute = $listing->get_your_attribute_field_name();
  
  if(!$custom_attribute){
   return $blocks;
  }
  
  return hivepress()->template->merge_blocks(
   $blocks,
   [
    'page_content' => [
     'blocks' => [
      'custom short' => [
       'type' => 'content',
       'content' => do_shortcode('['.$custom_attribute.']'),
       '_order' => 80,
      ],
     ]
    ]
   ]
  );
 },
 1000,
 2
);

I would also like to use this reviews plugin as a custom listing attribute. I created a custom attribute called bookingreviews. So, can you explain how I make this work? Do I leave the first code snippet provided by alalcarmac, and then add andrii’s code snippet? If so, do I just change, the area where it says get_your_attribute_filed_name to bookingreviews? I tried that but got a critical error message.

It would be great if i can get the widget to work as a custom listing attribute. As then the listing owners can put their airbnb and booking reviews per listing. Please explain how can I get it to work or what am I missing here in the steps. Thanks!

If there’s a $listing object, you can get this attribute this way:

$listing->get_bookingreviews();

But note that it will not contain the reviews or rating value unless there are some custom functions that set it and keep it updated.

1 Like

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