Hide unchecked attribute space on listing page?

How do I hide an attribute box on the listing page for which the user has not checked?


Please try this PHP snippet

add_filter(
	'hivepress/v1/models/listing/attributes',
	function( $attributes ) {
		$custom_snippet_primary_attributes = [];
		foreach($attributes as $name => $args){
			if(isset($args['display_areas']) && in_array('view_page_primary', $args['display_areas'])){
				$custom_snippet_primary_attributes[] = $name;
			}
		}
		
		hivepress()->request->set_context('custom_snippet_primary_attributes', $custom_snippet_primary_attributes);

		return $attributes;
	}
);

add_filter(
	'hivepress/v1/templates/listing_view_page/blocks',
	function( $blocks, $template ) {
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}
		
		$custom_snippet_primary_attributes = hivepress()->request->get_context('custom_snippet_primary_attributes');
		
		if(!$custom_snippet_primary_attributes){
			return $blocks;
		}
		
		$hide_primary = true;
		
		foreach ($custom_snippet_primary_attributes as $attribute){
			$value = call_user_func( [$listing, 'get_'.$attribute]);
			
			if($value){
				$hide_primary = false;
				break;
			}
		}
		
		if($hide_primary){
			$blocks = hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'listing_attributes_primary' => [
								'type' => 'content',
							],
						],
				]
				)['blocks'];	
		}
		
		return $blocks;
	},
	1000,
	2
);
1 Like

Thank you, I will try this.

Works great, thanks!