evan3168
evan3168

Reputation: 63

Switch not working within while loop (Wordpress PHP)

I've tried what I could on this and am still stuck, so I'm looking for some help. I'm sure there's something small I'm overlooking or am not aware of, so I'd be grateful for another set of eyes to take a look!

I'm trying to use a switch within a Wordpress while loop to set dimensions on post thumbnails for specific posts. The switch uses an auto-incrementing value ($count). Inside the loop, $count will return the right number for div ID's, but it will not work with the switch. All of the thumbnails go to the size defined before the loop begins (see $thumbsize)

Here's the code:

// Setup loop to pull only posts tagged slider
$max = 6;
$args = array('tag' => 'slider','posts_per_page' => $max);
$featuredPosts = new WP_Query();
$featuredPosts->query($args);

// Defaults for post thumbnail display
$thumbargs = array('class' => 'featured-blocks-img');
$thumbsize = array(640,360);

$count = 0;

    // Begin loop
    if ($featuredPosts->have_posts()) : while ($featuredPosts->have_posts()) : $featuredPosts->the_post();

    $count++;

    // Get post category and format for div class name
    $category = get_the_category();
    $catname = $category[0]->cat_name;
    $catdash = 'cat-';
    $catdash .= str_replace(' ', '-', $category[0]->cat_name);
    $catdash = strtolower($catdash);

    // Change post thumbnail size conditionally
    switch ($count) {
        case 2:
        case 5:
        case 6:
            $thumbsize == array(320,260);
            break;
        default:
            $thumbsize == array(640,360);
    } // End switch
    ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
        <div id="home-featured-post-<?php echo $count;?>" class="featured-blocks-post <?php echo $catdash; ?>">
            <h2 class="home-featured-title"><?php the_title(); ?></h1>
            <?php the_post_thumbnail($thumbsize, $thumbargs); ?>
        </div>      
    </a>

    <?php
    endwhile;
    endif; // End loop

And here it is in Gist form if that's helpful to anyone: https://gist.github.com/anonymous/8984741

I tried to add comments that would provide some context.

Any ideas of what's happening? I can provide the resulting HTML source if that would help also.

Upvotes: 0

Views: 463

Answers (1)

jswat
jswat

Reputation: 382

Looks like you aren't actually setting $thumbsize in the below code

$thumbsize == array(320,260);

== is comparing $thumbsize to that array, not creating an array with those values.

You really want it to just look like this:

$thumbsize = array(320,260);

Upvotes: 1

Related Questions