How to add Adsense ad after the header?

Hi all,

I am trying to embed adsense ad within my site after the header and before the content.

I want a simple code that i could add to functions.php with the adsense ad code.

Any assistance will be most welcome.

Many thanks.

Hey Mohammad,

Thanks for writing in!

To add your Google Ads between the header and before the content area, please add the following code in your child theme’s functions.php file

function add_goggle_ads(){ <?

   // place the Google Ad code here //

<?php }
add_action('x_before_view_global__slider-below', 'add_goggle_ads');
add_action('x_after_masthead_end', 'add_goggle_ads');

Please let us know if this works out for you.

It doesn’t work unfortunately … the site gives me the following error message whenever i add the code to the functions.php in the end of it.

The site is experiencing technical difficulties.

Please help

Hello Mohammad,

Thanks for writing in!

Please replate above code with following and let us know the outcome:

function add_goggle_ads(){
?>
      // place the Google Ad code here //

<?php
}
add_action('x_before_view_global__slider-below', 'add_goggle_ads');
add_action('x_after_masthead_end', 'add_goggle_ads');

If you would like to learn more about WordPress functions, please take a look at following resources:

https://codex.wordpress.org/Function_Reference

Thanks.

Thanks it works perfectly!:grin:

I have another question how could i display a different ad for the home page than other pages in the website?

Zatari

Hello Zatari,

To display different ad from the homepage, you will need to add a condition in the code just like this:

function add_goggle_ads(){
?>
	if ( is_front_page() && is_home() ) {
	  
	  // Default homepage
	  // place the Google Ad code here //

	} elseif ( is_front_page() ) {
	  
	  // static homepage
	  // place the Google Ad code here //
	
	} elseif ( is_home() ) {
	
	  // blog page
	  // place the Google Ad code here //
	
	} else {
	
	  //everything else
	  // place the Google Ad code here //
	
	}

<?php
}
add_action('x_before_view_global__slider-below', 'add_goggle_ads');
add_action('x_after_masthead_end', 'add_goggle_ads');

Please note that custom coding is outside the scope of our support. Issues that might arise from the use of custom code and further enhancements should be directed to a third party developer.

For more information about the different WordPress conditional tags, please check this out:

Thanks for your help…
How could i add custom html after the google ads?

Zatari

Hey Zatari,

You can use this code example to insert your Google ads and html code:

function add_goggle_ads(){ ?>
  <?php if ( is_front_page() && is_home() ) { ?>
    
    // Default homepage
    // place the Google Ad code here //
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

  <?php } elseif ( is_front_page() ) { ?>
    
    // static homepage
    // place the Google Ad code here //
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  
  <?php } elseif ( is_home() ) { ?>
  
    // blog page
    // place the Google Ad code here //
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  
  <?php } else { ?>
  
    //everything else
    // place the Google Ad code here //
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  
  <?php }
}
add_action('x_before_view_global__slider-below', 'add_goggle_ads');
add_action('x_after_masthead_end', 'add_goggle_ads');

Hope this helps.

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