wordpress switch statement within the loop

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

Related posts

Leave a Reply

5 comments

  1. 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 the global $intCounter; is unnecessary.

  2. 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:

    <?php
    
    query_posts('category_name=clients&posts_per_page=3&tag=new-work');
    
    if(have_posts()) {
        $intcounter = 0; // Moved this to within the IF block
    
        while(have_posts()){
            // If you did want to use the "global" keyword, you'd probably use it here:
            // global $intcounter;
    
            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:  // Curly braces not required here.
                    $ThisPostCSSClass="noclass";
            } // Switch
    
    ?>
    <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
    
        } // While
    
    } // If
    ?>
    
  3. 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.

  4. 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.

  5. Do not use “:” for your stamentlists, but use Curly brackets { }.

    Eg:

    if(have_posts()) {
       while(have_posts()) {
           the_post(); 
           $intcounter++;
           switch ($intcounter){
               case 1:
                 ...
                 ...
           }
       }
    }