Add block above description listing

hello, is it possible to add a block containing html before the description? I would like to integrate an adsense script.

For info I already have this code placed before the description:

add_filter(
	'hivepress/v1/templates/listing_view_page/blocks',
	function( $blocks, $template ) {
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}
		
		$text = $listing->display_avis_knzd();
		
		if(!$text){
			return $blocks;
		}
		
		return hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'page_content' => [
								'blocks' => [
									'custom_text_above_description' => [
										'type' => 'content',
										'content' => '<p>'.$text.'</p>',
										'_order' => 55,
									],
								],
							],
						],
				]
				)['blocks'];
		},
	1000,
	2
);

Thanks

Please try this PHP snippet instead. Also, please make sure that the listing has an attribute with the field name avis_knzd, as you have tried to display the value of this attribute in your code snippet

add_filter(
	'hivepress/v1/templates/listing_view_page/blocks',
	function( $blocks, $template ) {
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}
		
		$text = $listing->get_avis_knzd();
		
		if(!$text){
			return $blocks;
		}
		
		return hivepress()->template->merge_blocks(
					$blocks,
					[
						'page_content' => [
								'blocks' => [
									'custom_text_above_description' => [
										'type' => 'content',
										'content' => '<p>'.$text.'</p>',
										'_order' => 55,
									],
								],
							],
				]
				);
		},
	1000,
	2
);

Thanks @yevhen

The problem is that I need a second block because the 1st ā€œavis_knzdā€ is not displayed on all the listings. The ā€œadsenseā€ block must appear on all listings before the description.

If possible on this order:

  1. Block ā€œadsenseā€
  2. Block description native to hivepress
  3. Block ā€œavis_knzdā€

If you want to show the listing attribute with the field name adsense before the listing description on the listing page, then please change $listing->get_avis_knzd() on $listing->get_ adsense() in the last code snippet

I think I expressed myself badly.

  1. I would like this code to be integrated on all listings (without creating a new attribute and having to manually integrate adsense into each listing) before the description:
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="fluid"
     data-ad-layout-key=""
     data-ad-client=""
     data-ad-slot=""></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
  1. Display the attribute ā€œavis_knzdā€ (code previously provided) after the description actually display before

I hope I was a little more understandablešŸ˜…

  1. Sorry for the inconvenience, but customization is beyond our support scope - it includes fixing bugs and guidance about the available features Support Policy | HivePress

If customizations are required for your site, please try customizing it using the collection of code snippets Search Ā· user:hivepress Ā· GitHub and other developer resources, or consider hiring someone for custom work https://fvrr.co/32e7LvY

  1. Please try this PHP snippet
add_filter(
	'hivepress/v1/templates/listing_view_page/blocks',
	function( $blocks, $template ) {
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}
		
		$text = $listing->get_avis_knzd();
		
		if(!$text){
			return $blocks;
		}
		
		return hivepress()->template->merge_blocks(
					$blocks,
					[
						'page_content' => [
								'blocks' => [
									'custom_text_above_description' => [
										'type' => 'content',
										'content' => '<p>'.$text.'</p>',
										'_order' => 70,
									],
								],
							],
				]
				);
		},
	1000,
	2
);

Thank you for the answer,

  1. Ok, is it possible to put a shortcode before the description instead? Maybe it’s less complex? it would be nice to see in the next updates, the possibility of integrating adsense spaces in the theme…

  2. Thank you, I’ll try that soon

Please try to use this PHP snippet to add some shortcode before the listing description

add_filter(
	'hivepress/v1/templates/listing_view_page/blocks',
	function( $blocks, $template ) {
		return hivepress()->template->merge_blocks(
			$blocks,
			[
				'page_content' => [
					'blocks' => [
						'custom_listing_shortcode' => [
							'type' => 'content',
							'content' => do_shortcode('[your_shortcode_here]'),
							'_order' => 59,
						],
					],
				],
			]
		);
	},
	1000,
	2
);
2 Likes

awesome, works amazing to display adsense ads via shortcode.

Always on top of your services, I love it

1 Like

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