Snippets: Insert Advertisement (Adsense) or Banner Above Blog Posts

Using theme hooks you can easily add any code above your post content. This includes an advertisement banner or Adsense. Here is a sample snippet of code showing how you can add a banner at the top of all blog posts. If you want a standard image banner simply replace the link href attribute with your advertisement link and the img src attribute with your image URL. If you wanted to display Adsense, BuysellAds or another custom code simply delete the whole link HTML and dump your custom code in there.

add_action( 'wpex_hook_primary_before', function() {

	// Bail if not a singular post
	if ( ! is_singular( 'post' ) ) {
		return;
	} ?>

	<div class="container textcenter" style="margin-bottom:30px;">

		<a href="https://total.wpexplorer.com/"><img src="https://demo.wpexplorer.com/today/wp-content/themes/wpex-today/images/banner.png"></a>

	</div>

<?php } );

Result:

Snippet Type: PHP

Posted Under: Blog

All PHP snippets should be added via a child theme's functions.php file.