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

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程