Put Adsense between post in homepage
-
Hi I want put some adsense ads in the home of my page, but I want this
One ad in the top of the page, below of the tittle and before of the first page.
One ad after the first post.
Other ad after the second post.
I tried this https://wordpress.org/support/topic/adsense-ads-after-2-3-home-posts?replies=4
But in mobile the ads appear after the two first post, and the ads appear one after another and I want that it appear intercalated.
I hope anyone can help me or guide me.
Thanks
-
Hi Vivisa0290. Welcome to the Hueman forum. Please post a link to your site so we can take a look at the issue. Thanks.
Hi bdbrown, this is my site http://peliculasinfantiles.co/
The default Blog layout displays two posts per row, which is what you see in the desktop view. Then you have two ads inserted below those posts. In mobile view the layout still displays two posts per row but, due to the viewport width, the posts are stacked in one column. So, you still see two posts followed by the two ads. The page structure doesn’t change based on the viewport.
If you wants the ads side-by-side in the mobile view you could try adding this to your child theme style.css file:
/* ads side-by-side in mobile view */ @media only screen and (max-width: 791px) { .my-ad-widget { width: 50%; } }Thanks bdbrown, but I don’t want that the ads appear the two in same screen sibe-by-side, I want that the ads appear in mobile one below the first post, and the second below the second post.
Something like this:
Post 1
———
|xxxxxxxxx|
|xxxxxxxxx|
|xxxxxxxxx|
———
Ad 1
———
|xxxxxxxxx|
|xxxxxxxxx|
|xxxxxxxxx|
———
Post 2
———
|xxxxxxxxx|
|xxxxxxxxx|
|xxxxxxxxx|
———
Ad 2
———
|xxxxxxxxx|
|xxxxxxxxx|
|xxxxxxxxx|
———I can do this with the Adsense plugin but I don’t use this.
I hope you can help me and sorry for my bad english.Then you need to do two things:
1. Enable Theme Options > Blog > Standard Blog List so the posts are displayed in a single column.
2. If you copied the code from the other post you linked, change it to this:
<?php if ( ot_get_option('blog-standard') == 'on'): ?> <?php $i = 1; ?> <!-- initialize the post counter --> <?php while ( have_posts() ): the_post(); get_template_part('content-standard'); echo '<div class="my-ad-widget">'; // add a container dynamic_sidebar('my-post-ads'); // insert the ad sidebar echo '</div>'; // close container endwhile; ?> <?php else: ?>Thanks again for the answer bdbrown, but I want keep the two columns in the blog, if I enter in the site in pc destopk I want see 2 columns with the ads after the first row with the two first posts, but if I enter in mobile I want see in the way that I say post 1, ad 1, post 2, ad 2.
I’m trying to do this because with the adsense plugin I could do it, but coding I don’t know how. I try to put the ad right after of the post and in mobile it works but the problem is that in destopk the ad appear in the right (not below the post how I want) and the post in the left.
I hope you can help again 🙂
OK, see if this does what you’re after:
1. Set Theme Options > Blog > Standard Blog List OFF
2. In your child theme index.php file find this block of code:
<div class="post-list group"> <?php $i = 1; echo '<div class="post-row">'; while ( have_posts() ): the_post(); ?> <?php get_template_part('content'); ?> <?php if($i % 2 == 0) { echo '</div><div class="post-row">'; } $i++; endwhile; echo '</div>'; ?> </div><!--/.post-list-->And replace it with this block of code:
<div class="post-list group"> <?php $i = 1; echo '<div class="post-row">'; while ( have_posts() ) { the_post(); get_template_part('content'); // display ad after every post for mobile view echo '<div class="my-ad-widget-mobile">'; dynamic_sidebar('my-post-ads'); echo '</div>'; if($i % 2 == 0) { // display two ads after every post row for desktop view echo '<div class="my-ad-widget-desktop">'; dynamic_sidebar('my-post-ads'); echo '</div>'; echo '<div class="my-ad-widget-desktop">'; dynamic_sidebar('my-post-ads'); echo '</div>'; echo '</div><div class="post-row">'; } $i++; } echo '</div>'; ?> </div><!--/.post-list-->3. Add the following to your child theme style.css file:
/* float ads to left side of content area */ .my-ad-widget-desktop, .my-ad-widget-mobile { float: left; } /* default mobile ads to hidden */ .my-ad-widget-mobile { display: none; } /* on mobile hide desktop ads and display mobile ads */ @media only screen and (max-width: 720px) { .my-ad-widget-desktop { display: none; } .my-ad-widget-mobile { display: block; } }Thanks a lot bdbrown it works perfectly. 🙂
Finally I want know how can I put another block like this in the header before the first post. Is it posible?
in the header before the first post
Those are two different locations. “In the header” you could use the theme Header Ads widget which would then show on each page, unless you used css to hide it on specific pages. “Before the first post” would be in the content area and require additional modifications to the index.php file.
I’m interesting in the Ad before de first post. What I have to do?
In the top part of the code for index.php, find this:
the_post(); get_template_part('content');And replace those two lines with this:
the_post(); if ($i == 1) { // put one ad before first mobile post echo '<div class="my-ad-widget-mobile">'; dynamic_sidebar('my-post-ads'); echo '</div>'; // display two ads before first post row in desktop view echo '<div class="my-ad-widget-desktop">'; dynamic_sidebar('my-post-ads'); echo '</div>'; echo '<div class="my-ad-widget-desktop">'; dynamic_sidebar('my-post-ads'); echo '</div>'; echo '</div><div class="post-row">'; } get_template_part('content');That work, thanks 🙂
Glad it’s working. If you don’t need any further assistance here please mark the topic as Resolved. Thanks.
The topic ‘Put Adsense between post in homepage’ is closed to new replies.
