How to add tags on a single listing block?

Hello! I wondered how to add tags on a single listing block instead of just showing categories. I have tried many snippets that I found, but none of them worked. I want the block to look like the capture. Please help me with the PHP code. Thanks!

I finally figured it out! And I leave the snippets here for anyone who wants to figure out a similar problem like me.

add_filter( 
	'hivepress/v1/templates/listing_view_block/blocks', 
	function( $blocks, $template ) {
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}
		
		$tags = $listing->get_tags();
		$tag_view = [];
		
		foreach((array)$tags as $tag){
			$tag_view[] = '<a href="' . esc_url( $tag->get_url() ) . '" class="tag-cloud-link">' . esc_html( $tag->get_name() ) . '</a>';
		}
	
		return hivepress()->helper->merge_trees(
			[ 'blocks' => $blocks ],
			[
				'blocks' => [
					'listing_content' => [
						'blocks' => [
							'listing_tags' => [
								'type'   => 'content',
								'content'  => '<div class="hp-listing__tags hp-section tagcloud">' . implode(' ', $tag_view) . '</div>',
								'_order' => 40,
							],
						],
					],
				],
			]
		)['blocks'];
	},
	1000,
	2
);
4 Likes

Please try replacing this code:

$tag->get_url()

with:

get_term_link( $tag->get_id() )

It should be ok then, I tested it locally.

2 Likes

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