Reputation: 1487
This is for a wordpress project but is more PHP really. I'm executing a query to bring back a max of 12 posts. Each set of three posts is wrapped in a div entitled jobsN
. Each post (item) within that div comes back as item1
, item2
and item3
.
The item1
class works just fine but the jobs
class doesn't come back as 1-2-3-4. It comes back as 1-0-0-0. I can't work out what is going wrong with my count.
As you can see from the HTML below, there is something off with the jobs count. This part is handled here:
<?php if ($count == 3) {?></div><?php $count = 0; ?><div class="jobs
<?php echo $count; ?>"><?php }; ?>
Here is the full query:
<?php
query_posts(array(
'post_type' => 'custom_job',
'showposts' => 12
) );
?>
<?php if ( have_posts() ): $contcount = 0; $count = 0;?>
<?php $contcount++; ?>
<div class="jobs<?php echo $contcount; ?>">
<?php while (have_posts()) : the_post(); $count++;?>
<div class="item-<?php echo $count ?>">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<p><?php echo get_the_excerpt(); ?></p>
<a href="<?php the_permalink() ?>">Learn more</a> -->
</div>
<?php if ($count == 3) {?></div><?php $count = 0; ?><div class="jobs<?php echo $count; ?>"><?php }; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
Here is the HTML I get back:
<div class="jobs1">
<div class="item-1">
content
</div>
<div class="item-2">
content
</div>
<div class="item-3">
content
</div>
</div>
<div class="jobs0">
<div class="item-1">
content
</div>
<div class="item-2">
content
</div>
<div class="item-3">
content
</div>
</div>
<div class="jobs0">
<div class="item-1">
content
</div>
<div class="item-2">
content
</div>
<div class="item-3">
content
</div>
</div>
<div class="jobs0">
<div class="item-1">
content
</div>
</div
Upvotes: 0
Views: 97
Reputation: 15912
You're using the wrong counter in your PHP:
Your code has this line:
<?php if ($count == 3) {?></div><?php $count = 0; ?><div class="jobs<?php echo $count; ?>"><?php }; ?>
And it should be
<?php if ($count == 3) { ?></div><?php $count = 0; $contcount++; ?><div class="jobs<?php echo $contcount; ?>"><?php }; ?>
Because $count
stores the item, and $contcount
stores the block of items
Upvotes: 0
Reputation: 11690
Try with this, not 100% sure it will work
<?php
query_posts(array(
'post_type' => 'custom_job',
'showposts' => 12
) );
?>
<?php if ( have_posts() ): $contcount = 0; $count = 0;?>
<?php $contcount++; ?>
<div class="jobs<?php echo $contcount; ?>">
<?php while (have_posts()) : the_post(); $count++;?>
<div class="item-<?php echo $count ?>">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<p><?php echo get_the_excerpt(); ?></p>
<a href="<?php the_permalink() ?>">Learn more</a>
</div>
<?php if ($count == 3): ?>
<?php $count = 0; $countcount++;?>
<div class="jobs<?php echo $contcount; ?>"></div>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
EDIT: Edited the possible error.
Upvotes: 0
Reputation: 691
This line is the problem:
<?php if ($count == 3) {?></div><?php $count = 0; ?><div class="jobs<?php echo $count; ?>"><?php }; ?>
If $count = 3 it means you will desplay a new jobs div but you also make $count = 0 then you are echoing it as 0.
Transform this line to:
<?php if ($count == 3) {?></div><?php $count = 0; $countcount++; ?><div class="jobs<?php echo $countcount; ?>"><?php }; ?>
Upvotes: 1
Reputation: 9635
use this line
<?php if ($count == 3) {?></div><?php $count = 0; $contcount++; ?><div class="jobs<?php echo $contcount; ?>"><?php }; ?>
instead of
<?php if ($count == 3) {?></div><?php $count = 0; ?><div class="jobs<?php echo $count; ?>"><?php }; ?>
Solution :
I have updated the $contcount++
when $count==3
and use in echo class jobs<?php echo $contcount
Upvotes: 1