How to insert Adsense ads between articles on the homepage [WordPress]

In this tutorial we will know how to insert Adsense blocks between the articles of the home page of your blog for the famous Genesis theme. We will also choose to insert these ads after a number of articles of our choice and all this without a plugin, with a simple insertion of code in the function file of the child theme.In the image below an example of our final result:

inerer-adsense-between-article

Insert Adsense ads between articles on the homepage

To make it simple we will add the code below in the functions.php file of your child theme:

//* Add widget area between and after 3 posts
 
add_action( 'genesis_after_entry', 'wbxp_between_posts_area' );
function wbxp_between_posts_area() {
global $loop_counter;
$loop_counter++;
if( $loop_counter == 3 ) {
if ( is_active_sidebar( 'between-posts-area' ) ) {
echo '<div class="wbxp-google-ads">';
dynamic_sidebar( 'between-posts-area' );
echo '</div><!-- end .top -->';
}
$loop_counter = 0;
}
}

Then add this code:

//* Add new widget area between posts of home page
 
genesis_register_sidebar( array(
'id' => 'between-posts-area',
'name' => __( 'Between Posts Area', 'genesis-theme' ),
'description' => __( 'This widget show between and after few posts.', 'genesis-theme' ),
) );

After adding this last code, a widget called ”  between post area  ” will be created, in this widget you can add a text widget and then insert the Adsense code of your choice.

To insert the Adsense code after every 2 articles, change the number 3 (see the first code) in “ loop_counter == 3  ” by 2 which will insert the Ads code after every two Articles. For my case I chose to put 6 articles in my homepage.

Finally to adjust the position of your advertising block add this code to your CSS theme:

.wbxp-google-ads {
text-align: center;
margin-top: -27px;
margin-bottom: -37px;
}

If you liked this article, please subscribe to our reddit community to discuss it. You can also find us on Twitter and Facebook.

5/5 - (1 vote)
See also  Yoast Seo Plugin: ultimate configuration to do (2022)
SAKHRI Mohamed
SAKHRI Mohamed

The blog of a computer enthusiast who shares news, tutorials, tips, online tools and software for Windows, macOS, Linux, Web designer and Video games.

Articles: 3764

Leave a Reply

Your email address will not be published.