WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Place Ads Between Posts (5 posts)

  1. IlanK
    Member
    Posted 1 year ago #

    Hi, I'd like to place ad units between posts on the front page.
    I know I have to put the code in the loop.php.
    I would like the ads to appear between the three posts that I have on the front page (so two ads). I have only been able to get it so that three ads appear, between posts and also one at the end. How do I make it so that no ads appear at the end, but just in between posts?

    Here is my loop.php code:

    <?php /* If there are no posts to display, such as an empty archive page */ ?>
    <?php if ( ! have_posts() ) : ?>
    		<h1><?php _e( 'Not Found', 'ari' ); ?></h1>
    		<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 
    
    'ari' ); ?></p>
    
    <?php endif; ?>
    
    <?php /* Start the Loop. */ ?>
    <?php while ( have_posts() ) : the_post(); ?>
    
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
    	<?php if (is_sticky()) echo __( '<h3 class="sticky-label">Featured</h3>', 'ari' ); ?>
    
    <?php the_time('l, F d, Y') ?>
    		<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'ari' ), the_title_attribute( 'echo=0' ) ); 
    
    ?>" rel="bookmark"><h2><?php the_title(); ?></h2></a>
    <?php _e( 'Written by', 'ari' ); ?> <?php the_author() ?> // <?php comments_popup_link( __( 'Leave a comment', 'ari' ), __( 'One comment', 'ari' ), 
    
    __( '% comments', 'ari' ) ); ?>
    <br>
    <br>
    	<?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
    			<?php the_excerpt(); ?>
    			<p class="meta"><span><a href="<?php the_permalink(); ?>">
    	<?php else : ?>
    			<?php the_content( __('Read more >>', 'ari' ) ); ?>
    			<div class="clear"></div>
    
    			<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'ari' ), 'after' => '</div>' ) ); ?>
    
    	<?php endif; ?>
    <?php if ( count( get_the_category() ) ) : ?>
    				<?php endif; ?>
    	</div>
    	<!--end Post-->
    <div class="ad">
    <script type="text/javascript"><!--
    google_ad_client = "ca-pub-2637633606355131";
    /* left ad square */
    google_ad_slot = "1791464294";
    google_ad_width = 250;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    </div>
    
    		<?php comments_template( '', true ); ?>
    
    <?php endwhile; // End the loop. ?>
    
    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if (  $wp_query->max_num_pages > 1 ) : ?>
    				<p class="previous"><?php next_posts_link( __( '&larr; Older posts', 'ari' ) ); ?></p>
    				<p class="next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'ari' ) ); ?></p>
    <?php endif; ?>

    Thank you so much!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    you can use the variable $wp_query->current_post (starts with 0 zero, and indicates the position of the post in the loop) in a conditional statement;

    example:

    <!--end Post-->
    <?php if( $wp_query->current_post < 2 ) : ?>
    <div class="ad">
    <script type="text/javascript"><!--
    google_ad_client = "ca-pub-2637633606355131";
    /* left ad square */
    google_ad_slot = "1791464294";
    google_ad_width = 250;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    </div>
    <?php endif; ?>
  3. IlanK
    Member
    Posted 1 year ago #

    Thanks, that works great!

  4. sid5491
    Member
    Posted 1 year ago #

    i am still finding problems with inserting these ads . help me
    here http://www.exchangemystuff.in/

  5. WPyogi
    Volunteer Moderator
    Posted 1 year ago #

    @sid5491 - looks like you are using a commercial theme (those are not supported on these forums), so you'll need to ask the developer of your theme for help -

    http://themeforest.net/item/bazar-shop-multipurpose-ecommerce-theme/3895788

Topic Closed

This topic has been closed to new replies.

About this Topic