Ritzy Ritzy - 5 months ago 15
PHP Question

Putting alternative text after every third post

I am currently working on a

WordPress
site for the first time and having some issues figuring out how to achieve what I am aiming for. I would like for it to display a banner advertisement after every third post then continue from where it left off in my existing post.

Because I probably worded this poorly I will display an example image below.



Here's the current loop file I am using within my
index.php
file.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php

$title = htmlentities(get_the_title ());
$str = explode ("&amp;#8211;", $title);

$artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);

?>
<div class="album-meta" style="border-bottom: 1px solid #eee;">
<div class="cover">
<a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>"></a>
</div>
<div class="metadata">
<a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p>
<p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p>
</a>
<p><a href="http://linkshrink.net/zPog=<?php the_permalink(); ?>" style="color: #fff; background: #4E76C9; width: 200px; height: 50px;padding: 5px;line-height: 50px;font-size: 20px;font-weight: bold; border: none;text-shadow: 0px 1px 0px #3170DD;box-shadow: inset 0px 0px 0px 1px #3170DD;border-radius: 3px 3px; cursor: pointer; text-decoration: none;">Download</a>
</div>
</div>
<?php endwhile; else : ?>



<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>



<?php endif; ?>

Answer

You can do it like below:-

<?php $i = 0; // create a counter?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 

<?php 

    $title = htmlentities(get_the_title ()); 
    $str = explode ("&amp;#8211;", $title);

    $artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
    $song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);

?>
<div class="album-meta" style="border-bottom: 1px solid #eee;">
    <div class="cover">
        <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>"></a>
    </div>
    <div class="metadata">
        <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p>
        <p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p>
        </a>
        <p><a href="http://linkshrink.net/zPog=<?php the_permalink(); ?>" style="color: #fff; background: #4E76C9; width: 200px; height: 50px;padding:  5px;line-height: 50px;font-size: 20px;font-weight: bold; border: none;text-shadow: 0px 1px 0px #3170DD;box-shadow: inset 0px 0px 0px 1px #3170DD;border-radius: 3px 3px; cursor: pointer; text-decoration: none;">Download</a>
    </div>
</div>
<?php if($i%3 ==0 && $i >0){ // check you reached to third div or not?>
<!-- write the html of advertisement div ------->
<?php $i++;} ?> <!-- increase counter -->
<?php endwhile; else : ?>



<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>



<?php endif; ?>
Comments