Trouble getting listing attribute in functions.php

Hi all,
I want to get a custom listing attribute in functions.php. I am pretty sure that I have an attribute called “country”, but this code does not show anything in my listing block.
Any ideas?


add_filter(
	'hivepress/v1/templates/listing_view_block/blocks',
	function ($blocks, $template){
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}

		$country = $listing->get_country();
  
		if(!$country){
		 return $blocks;
		}

		return hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'listing_details_primary' => [
								'blocks' => [
									'listing_country' => [
										'type'   => 'content',
										'content'  => '<a href="#">'.$country.'</a>',
										'_order' => 40,
									],
								],
							],
						],
				]
				)['blocks'];
	},
	1000,
	2
);

Please send more details that may help to detect or reproduce this issue (e.g., screenshots, the type of the Country attribute (e.g., text, select, checkboxes, etc.) or the error message you get).

Thank you for your response and apologies, I am quite new to inquiries on dev forum, so let me be more specific.

I am creating a job listings portal. The aim is to show “country” where the vacancy is based as part of the listing block.

Country attribute is published and has following parameters:

The attribute has several options like “Germany”, “Austria”, “United Kingdom”, etc. Not all listings have a country since I am just working with dummy data, but some certainly do.

I know that this could be done via Attribute >> Areas, but my point is to insert it manually (programatically) because it gives me more formatting options. There is no error message, but the code I shared leaves the $country variable empty (it shows only the icon but no country value).

Current code:

add_filter(
	'hivepress/v1/templates/listing_view_block/blocks',
	function ($blocks, $template){
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}

		$country = $listing->get_country();
  
		/*if(!$country){
		 return $blocks;
		}*/

		return hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'listing_details_primary' => [
								'blocks' => [
									'listing_country' => [
										'type'   => 'content',
										'content'  => '<div><i class="hp-icon fas fa-map-marker"></i><a href="#">'.$country.'</a></div>',
										'_order' => 40,
									],
								],
							],
						],
				]
				)['blocks'];
	},
	1000,
	2
);

Please try this PHP snippet

add_filter(
	'hivepress/v1/templates/listing_view_block/blocks',
	function ($blocks, $template){
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}

		$country = $listing->display_country();
		
		if(!$country){
		 return $blocks;
		}
		
		return hivepress()->template->merge_blocks(
			$blocks,
			[
				'listing_details_primary' => [
					'blocks' => [
						'listing_country' => [
							'type'   => 'content',
							'content'  => '<div><i class="hp-icon fas fa-map-marker"></i><a href="#">'.$country.'</a></div>',
							'_order' => 40,
						],
					],
				],
			]
		);
	},
	1000,
	2
);

It works! Thank you very much for your help.

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