In this tutorial we are going to create new widget area
in most recent post below title in WordPress and in genesis.
This helps WordPress users to keep the static image or mostly use adsense code to display the largest leader board banner which looks elegant in your blog for ads.
I have not yet integrated adsense in my blog but i have already kept the widget area to manage them later so it could be more easy for me to just drag and drop the adsense codes into the specified widget area.
Lets get Started.
What we are going to do ?
- Register new widget area named as after post title.
- Set the Widget area below post title so that whenever we keep anything inside widget, it will displays them below post title in our theme.
Part 1 : ( For Genesis Users Only )
Step 1:-
Register new widget area. Open your functions.php
file and write this code at the end of it. :-
1 2 3 4 5 6 |
//* Add After Post Title Widget genesis_register_sidebar( array( 'id' => 'ad-after-post-title', 'name' => __( 'Ad After Title', 'custom' ), 'description' => __( 'This is Ad section under post title', 'custom' ), ) ); |
Step 2 :-
Set this new widget area below post title.
1 2 3 4 5 6 7 8 9 10 |
//* Show ads below post title on posts add_action( 'genesis_entry_header', 'cih_post_after_title', 12 ); function cih_post_after_title() { global $wp_query; if ( is_single() && is_active_sidebar( 'ad-after-post-title' ) || (is_home() && is_active_sidebar('ad-after-post-title') ) && $wp_query->current_post == 0 ){ echo ' <div class="ad-after-post-title">'; dynamic_sidebar( 'ad-after-post-title' ); echo '</div>'; } } |
Thats it, now if you drag and drop new widgets into your newly created widget area, it will display below post title.
Put your own adsense ads or your custom ads, just drag and drop and keep your ads. These ads will show in single page as well as recent post in homepage.
If you want to remove from homepage just remove the line :-
1 |
(is_home() && is_active_sidebar('ad-after-post-title') |
This will remove the widget area from the most recent post on homepage and just show in single page only
.
part 2 :- ( For Normal WordPress themes )
step 1 :-
Step 1 is same. Just copy code from above or if you want to know more rely in WordPress documentation on wordpress.org.
Step 2:-
Lets first display widget area below title in your single post only. Find your post page called single.php
and open it. Find your post loop and enter these codes just below the the_title();
1 2 3 4 5 6 |
global $wp_query; if ( is_single() && is_active_sidebar( 'ad-after-post-title' )){ echo '<div class="ad-after-post-title">'; dynamic_sidebar( 'ad-after-post-title' ); echo '</div>'; } |
Step 3 :-
Now same goes for index.php
as well, open up and add this line of code just below the the_title();
in your post loop to display widget area in most recent post in homepage.
1 2 3 4 5 6 |
global $wp_query; if(is_home() && is_active_sidebar('ad-after-title') ) && $wp_query->current_post == 0 ){ echo '<div class="ad-after-post-title">'; dynamic_sidebar( 'ad-after-post-title' ); echo '</div>'; } |
If you have any problem. Feel free to leave via comments section.
Leave a Reply