douruoshen1449 2014-02-13 22: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 22: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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址