Do you want to display advertisement after a specific number of posts on the blog page? This can be achieved in the Astra theme using custom code. Besides executing ad code, you can use the same code logic to display any content between posts listing on the blog page.
Display ads between posts on Blog Page
In the WordPress Dashboard, go to Appearance > Theme Editor and open the functions.php file of your Astra chid theme. Then add the following code at the end. Alternatively, you can use code snippets plugin for adding custom code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
add_action( 'astra_entry_before', 'wpd_astra_entry_after' ); function wpd_astra_entry_after() { if( is_home() ) { global $wp_query; if( $wp_query->current_post == 3 || $wp_query->current_post == 7 ) { echo '<div class="ads"> ENTER YOUR ADS SCRIPT HERE </div>'; } } } |
The above code will execute and display advertisement after the 3rd and 7th post. You can change 3 and 7 in the code for specific ad location as per requirement. Also, replace “Enter your Ads script here” with the actual advertisement code or contents that you want to display.
For posts on Archive & Category Pages
In addition to the blog page, if you want to display advertisements between posts on the archive and category pages, then use the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
add_action( 'astra_entry_before', 'wpd_astra_entry_after' ); function wpd_astra_entry_after() { if( is_home() || is_archive() || is_category() ) { global $wp_query; if( $wp_query->current_post == 3 || $wp_query->current_post == 7 ) { echo '<div class="ads"> ENTER YOUR ADS SCRIPTS HERE </div>'; } } } |
In the above code, there are two new conditional logic is_archive() & is_category() into the IF statement for executing this on archive and category pages.
Leave a Reply