I’m attempting to use a switch statement within the worpdpress loop to change classes on a div, but the incrementing counter ($IntCounter) doesn’t seem to be firing within the loop:
<?php
global $intCounter;
$intcounter = 0;
query_posts('category_name=clients&posts_per_page=3&tag=new-work');
if(have_posts()) : while(have_posts()) : the_post();
$intcounter++;
switch ($intcounter){
case 1:
$ThisPostCSSClass ="new-work-post span-7 colborder ";
break;
case 2:
$ThisPostCSSClass ="new-work-post span-8 colborder ";
break;
case 3:
$ThisPostCSSClass ="new-work-post span-7 last";
break;
default:{
$ThisPostCSSClass="noclass";
}
}
?>
<div class="<?php echo $ThisPostCSSClass;?>" id="<?php the_ID(); ?>">
<div class="">
<?php the_content(); ?>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php the_excerpt(); ?>
</div>
</div> <!-- .post -->
<?php endwhile;endif; ?>
Am I missing something obvious? Thanks
you are grabbing the global
$intCounter;
but setting and incrementing$intcounter;
Not sure this is the problem because you’re initializing
$intcounter=0;
and incrementing it correctly. So this only means theglobal $intCounter;
is unnecessary.I have a feeling this has something to do with your usage of
global
. Normally it’s used within a scope to tell it that you want to use the globally defined version of the variable and not the local one.I went ahead and redid the structure of the code blocks (for aesthetics, please humor me) with curly braces and removed the global keyword. Try giving this chunk a try and see if it works for you:
Why branches around default, and for the switch, but not for the if nor the while? Makes the code much harder to read. Also not sure why you’re using a global for $intcounter (which btw has a C instead of a c), unless there’s a reason for that that I dont know. Other than that seems that it should work.
You got a capital C in your global $intCounter but you are incrementing and switching on $intcounter. This initializes 2 different variables.
The switch statement and loop works fine otherwise.
Do not use “:” for your stamentlists, but use Curly brackets { }.
Eg: