• Hi everyone,

    I’ve been trying to add a code to my site’s index.php file in order to have ads displayed after every 3rd post. I’ve tried a number of options, but none has worked so far. I’m a beginner, so I often get confused with these lines of code. I’d really appreciate if someone could help!

    Here is what my index.php looks like

    <?php
    /**
     * The index template for displaying content
     *
     * @package Cryout Creations
     * @subpackage Tempera
     * @since Tempera 1.1
     */
    
    $options = tempera_get_theme_options();
    foreach ($options as $key => $value) { ${"$key"} = $value; }
    ?>
    
    		<section id="container" class="<?php echo tempera_get_layout_class(); ?>">
    
    			<div id="content" role="main">
    
    			<?php cryout_before_content_hook();
    
    			if ( have_posts() ) :
    
    				/* Start the Loop */
    				while ( have_posts() ) : the_post();
    
    					get_template_part( 'content/content', get_post_format() );
    
    				endwhile;
    
    				if($tempera_pagination=="Enable") tempera_pagination(); else tempera_content_nav( 'nav-below' );
    
    			else : ?>
    
    				<article id="post-0" class="post no-results not-found">
    					<header class="entry-header">
    						<h1 class="entry-title"><?php _e( 'Nothing Found', 'tempera' ); ?></h1>
    					</header><!-- .entry-header -->
    
    					<div class="entry-content">
    						<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'tempera' ); ?></p>
    						<?php get_search_form(); ?>
    					</div><!-- .entry-content -->
    				</article><!-- #post-0 -->
    
    			<?php
    			endif;
    			cryout_after_content_hook();
    			?>
    
    			</div><!-- #content -->
    		<?php tempera_get_sidebar(); ?>
    		</section><!-- #container -->

    I’m using tempera theme by cryout. THANKS!

Viewing 5 replies - 1 through 5 (of 5 total)
  • what options exactly have you tried?
    what were the results?

    one possibility:
    start by creating a child theme;
    then edit index.php in the child theme, and add some code after this line:

    get_template_part( 'content/content', get_post_format() );

    example code (for the same ad code after each 3rd post):

    if( $wp_query->current_post%3 == 0 && $wp_query->current_post > 0 && $wp_query->current_post <10 ) {
    //your code here//
    }

    something similar (needs to be adapted a bit): https://www.ads-software.com/support/topic/add-ads-between-posts-on-archive-lists-or-tags-list?replies=2#post-6376616

    for theme specific help, please post in https://www.ads-software.com/themes/tempera

    Thread Starter rafaelmanes

    (@rafaelmanes)

    I’ve tried your suggestion above, but that resulted in the blog page not being loaded at all below the header image and navigation menu.

    After that, I tried the suggestion you posted on https://www.ads-software.com/support/topic/add-ads-between-posts-on-archive-lists-or-tags-list?replies=2#post-6376616

    add_action( 'the_post', 'archive_adsense_injection' );
    
    function archive_adsense_injection() {
    	if( is_archive() ) {
    		global $wp_query;
    		if( $wp_query->current_post%3 == 0 && $wp_query->current_post >0 && $wp_query->current_post < 10 ) {
    			echo 'YOUR AD CODE';
    		}
    	}
    }

    But that only inserts an ad block on the Archive pages. I’d like to add that to the main blog page, where the posts are. What should I change on that code then?
    Thanks a millon
    Rafael

    But that only inserts an ad block on the Archive pages.

    that is why I said: ‘needs to be adapted’.

    try and remove the conditional:

    add_action( 'the_post', 'archive_adsense_injection' );
    
    function archive_adsense_injection() {
    	global $wp_query;
    	if( $wp_query->current_post%3 == 0 && $wp_query->current_post >0 && $wp_query->current_post < 10 ) {
    		echo 'YOUR AD CODE';
    	}
    }
    Thread Starter rafaelmanes

    (@rafaelmanes)

    Thanks again for your reply. When I removed that conditional line, my site stopped loading. I’d get a blank screen – panicked for a second, then went into the FTP and reverted the change.

    I tried with if( is_home() ) {
    …and it worked! Thank you! Now how do I set multiple conditionals: like is_home OR is-archive OR etc?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add ad code after every 3 posts’ is closed to new replies.