douruoshen1449 2014-02-13 14:05
浏览 39
已采纳

切换不在while循环中工作(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.

展开全部

  • 写回答

1条回答 默认 最新

  • douzhanglu4591 2014-02-13 14:13
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部