Reputation: 433
Hi there, I have a little bit of a problem. The front page of our new site has a series of thumbnail boxes with rollovers to open certain showcased products. The loop I have written shows all posts currently but will eventually be tied down only to the portfolio category.
My manager however wants a slider with recent blog posts after the third post and a couple of more sliders with references and suchlike after the 5th and 7th posts. I found a little bit of code that injects the same snippet every n posts but this is no good for me because I only want it to happen 3 times and all with different content, including one with a loop inside a loop (where presumably time will move mega slowly haha). Is this possible? If so, can anybody point me towards a code snippet?
Current Loop
<!-- Start of loop -->
<?php if (have_posts()) : ?>
<!-- Start of Post -->
<?php while (have_posts()) : the_post(); ?>
<!-- Check to see if there is featured image -->
<?php if (function_exists('has_post_thumbnail') && has_post_thumbnail()) { ?>
<?php $img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array( 960,960 )); ?>
<!-- End Checking -->
<div class="portfolioblock" style="background-image: url('<?php echo $img_src[0]; ?>');">
<a href="<?php the_permalink(); ?>">
<div class="rollover">
<div class="center">
<img src="<?php bloginfo('template_url'); ?>/img/zoom.png" alt="More" />
</div>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
</div>
</a>
</div>
<!-- Start Content Block -->
<?php } else { ?>
<div class="block">
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
</div>
<?php } ?>
<!-- End Content Block -->
<?php endwhile; ?>
<!-- End of Post -->
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>
<!-- End of Loop -->
And I found this code for injecting a code snippet every n posts.
<?php $postnum++; if($postnum%5 == 0) { ?>
YOUR AD CODE HERE
<?php } ?>
Upvotes: 1
Views: 1560
Reputation: 433
Elaborating on cweiske's answer:
Inserted just before endwhile
<?php $postnum++;
if ($postnum == 4) { ?>
<div class="block"><h2>Blog</h2></div>
<?php }
if ($postnum == 6) { ?>
<div class="block"><h2>References</h2></div>
<?php }
if ($postnum == 9) { ?>
<div class="block"><h2>Meet the team</h2></div>
<?php }; ?>
Upvotes: 1
Reputation: 1
Basically what you want to do is this.
Before the loop begins, initialise a counter variable to 0.
Each time the loop successfully finds a post, increment the variable.
Now, each loop, you will know how many posts have been displayed.
So at the appropriate place, you check the counter to see if it matches either 3 5 or 7. If it does match, you display the correct block.
Upvotes: 0
Reputation: 31146
You could count the number of posts that passed and then decide on that:
$postnum++;
if ($postnum == 3 || $postnum == 5 || $postnum == 7) {
echo 'foo';
}
Upvotes: 3