I want to put in and ad code after every 6 post on my blog. I cant really figure out how to break out of the foreach and insert the ad code.
This link will help you. Third title says: Insert Ads After The First Post
Change the code for 6 where it says 2:
<?php if (have_posts()) : ?> // Here we check if there are posts
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?> // While we have posts, add 1 to count
<?php if ($count == 6) : ?> // If this is post 6
//Paste your ad code here
<h2><?php the_title(); ?></h2> // post title
<?php the_excerpt(); ?> // You may use the_content too
<?php else : ?> // If this is not post 6
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
UPDATE: As Gordon noticed, you asked for code every 6 posts (sorry I missed that on my first read). So the code should be:
<?php if ($count % 6 == 0) : ?>
As what #Gordon commented, here's how I'd re-factor that code;
<?php if (have_posts()) : $count = 1; while (have_posts()): ?>
<?php if ($count == 6) : ?>
// Paste your ad code here
<?php $count = 0; endif; ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php $count++; endwhile; endif; ?>
Related
I have the following so far:
<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
<?php if ($count < 5) : ?>
//four posts here, all them wrapped in one div, these will also have different markup
<?php the_title();?>
<?php else : ?>
//remaining six posts here, all of them wrapped in one div, these will also have different markup
<?php the_title();?> blah
<?php the_excerpt();?>
<?php endif; ?>
<?php endwhile; ?>
I want to wrap the first 4 in a '<div class="top"></div>' and the bottom 6, I want all of them wrapped in a '<div class="bottom"></div>'
Any tips of how this can be done using the while loop would be greatly appreciated
Try the below snippet.
<?php
$count = 0;
while (have_posts()) : the_post(); $count++;
if ( $count == 1 ) echo '<div class="top">';
if ( $count > 5 ) echo '</div><div class="bottom">';
the_title();
if ( $count > 5 ) the_excerpt();
endwhile;
echo "</div>"
?>
This might help you.
<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
<?php if ($count < 5) : ?>
//four posts here, all them wrapped in one div, these will also have different markup
<div class="top">
<?php the_title();?>
</div>
<?php else : ?>
//remaining six posts here, all of them wrapped in one div, these will also have different markup
<div class="bottom">
<?php the_title();?> blah
<?php the_excerpt();?>
</div>
<?php endif; ?>
<?php endwhile; ?>
In Wordpress, I want to have a div wrapping around every three posts (because the posts are in a grid, three per line, and I want each line to have a uniform height so the "read more" buttons line up at the bottom - http://restartcomputer.com/category/products/mac-products/). I figured out (logically) how to do this - it is basically outlined in the accepted answer of this question: PHP loop: Add a div around every three items syntax
However, I've tried everything and cannot get the code to work. The divs do not get added, at all. Here is the code:
if (have_posts()) :
$counter = 1; ?>
<div class="entries-wrapper">
<?php while (have_posts()) : the_post(); ?>
//post stuff
<?php if ($counter % 3 == 0) { ?>
</div><div class="entries-wrapper">
<?php }
$counter += 1; ?>
<?php endwhile; ?>
</div>
//some more code
<?php endif; wp_reset_query(); ?>
Any idea why?
You have not entered back into Script (Line 4):
if (have_posts()) :
$counter = 1; ?>
<div class="entries-wrapper">
<?php while (have_posts()) : the_post(); ?>
//post stuff
<?php if ($counter % 3 == 0) { ?>
</div><div class="entries-wrapper">
<?php }
$counter += 1; ?>
<?php endwhile; ?>
</div>
//some more code
<?php endif; wp_reset_query(); ?>
I did something similar in my theme to align posts side by side:
<?php if ( have_posts() ) : ?>
<?php $col = 1; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if($col == 1) echo "<div class='row'>"; ?>
<div class="post col<?php echo $col; ?>" id="post-<?php echo the_ID(); ?>">
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php (($col==1)?$col=2:$col=1); ?>
</div>
<?php if($col == 1) echo "</div>"; ?>
<?php endwhile; ?>
<?php kubrick_paging_nav(); ?>
<?php else : ?>
Can also try moving the DIV within the WHILE statement.
This question already has answers here:
How to perform an action every 5 results?
(6 answers)
Closed 8 years ago.
I would like to put an adsense square (250x250) ad every ten posts in my posts list.
I tried adding this code to my index.php, inside of the posts div:
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 11) : ?>
Ad code is here
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php else : ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
It showed up correctly, but all the format of the posts changed for some reason.
Plus, this code only puts an ad after the tenth post, and it doesn't automatically repeat it after the twentieth, etc.
This is not actually really important, I could just repeat the numbers.
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
//////////////////////
<?php if($count%10==0){ echo "</br></br></br>"; } ?>
///////////////////
<?php if ($count == 11) : ?>
Ad code is here
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php else : ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
My client is looking to change her homepage to look similar to this site: http://www.eatliverun.com/
Where the most recent post is up top in full in one column and the others are in two column excerpts.
She wants to have just one recent post display and the rest in the two column format. I'm just not sure how to accomplish this correctly.
Her website is located at http://pamelastable.com/
Integrating a counter into your loop can easily help you achieve this layout. Try something as follows (note: the 'six columns' class represents your 2 column layout in a 12 column grid):
<?php $count = 1; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ($count == 1) { ?>
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
<?php $count++; ?>
<?php } else { ?>
<?php if($count % 2 == 0) { ?>
<div>
<? } ?>
<div class="six columns">
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
</div>
<?php if($count % 2 == 1) { ?>
</div>
<? } ?>
<?php $count++; ?>
<? } ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Just add a counter to the loop and style the html differently for the first iteration.
<?php $i = 0; while ( have_posts() ) : the_post(); //inside loop ?>
<?php if ($i++ == 0) : ?>
<div>First Post</div>
<?php else: ?>
<div>Other Posts</div>
<?php endif; ?>
<?php endwhile; ?>
In your index.php or other template file^
Just use a counter to see which post number you are on.
<?php
if (have_posts()) {
$i = 0;
while (have_posts()) {
the_post();
if (++$i == 1) {
echo '<div class="entry-large">';
} else {
echo '<div class="entry-small">';
}
the_content();
echo '</div>'
}
}
In my wordpress theme, I want to add in the footer a side loop to fetch the latest posts.
the first post of this loop displays thumb pix, title, and post preview..
the 5 following only displays the title/link.
Since I already use the regular <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
in the main div, I have to use a side loop based on get_posts()
this is what I want to get, but it's not working:
<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5');
$count = count($posts);
foreach ($posts as $post) : start_wp(); ?>
<?php if ($count < 2) : ?>
/// code for the 1st post (thumb etc..)
<?php else : ?>
/// code for the 4 following post (links to posts only)
<?php endif; ?>
<?php endforeach; ?>
I know how to add count/condition for the regular wordpress loop but not with the get_posts() function.
Can you please help me achieving that ?
Thanks in advance ;)
edit: the solution :
Ok I used the 'offset' argument to achieve that:
<?php $posts = get_posts('numberposts=2&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" class="footernews-thumb">
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(thumbnail); ?>
<?php endif; ?>
</a>
<h2 class="footernews-title"><?php the_title(); ?></h2>
<p class="footernews-preview"><?php the_content_rss('', TRUE, '', 20); ?></p>
<?php endforeach; ?>
<?php $posts = get_posts('numberposts=3&offset=1');
foreach ($posts as $post) : start_wp(); ?>
<h2 class="footernews-title"><?php the_title(); ?></h2>
<?php endforeach; ?>
Since the "loop" is not really a loop, I decided to avoid counting occurrence.
I would use a simple incrementing check as such....
<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5');
$i = 1;
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1) : ?>
/// code for the 1st post (thumb etc..)
<?php else : ?>
/// code for the 4 following post (links to posts only)
<?php endif; ?>
<?php ++$i;
<?php endforeach; ?>
<?php unset($i); ?>
This will then only give you the additional output for the first iteration, after that it will just keep going using the lesser code.
You're setting $count to the number of posts you're getting (5) so it will NEVER be less than 2.
You need to change it to this:
<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5');
$first = true;
foreach ($posts as $post) : start_wp(); ?>
<?php if ($first) : ?>
/// code for the 1st post (thumb etc..)
<?php $first = false; ?>
<?php else : ?>
/// code for the 4 following post (links to posts only)
<?php endif;?>
<?php endforeach; ?>