我找到了一些与if其他相关的代码,并尝试更改语法,但没有奏效


I found some code relating to if else, and tried changing syntax, but it didn't work

>更新:

感谢大家的精彩输入,我现在的代码在主页中正常工作。 仍然无法弄清楚为什么相同的代码在小部件中不起作用。 它正在拉下标题和永久链接,所以我知道它正在获取数据,但自定义值返回为零

我正在制作一个wordpress页面,使用该页面的小组正在接受观众的挑战。

挑战

页面是一个特殊模板,显示挑战完成程度的百分比条。

该栏工作正常,Chrome 中出现了一个小故障,但客户希望"待处理"的挑战完成率为 0%,"完成"为 100%,而不是图表。

我已经把这段代码放进了页面和一个小部件中,结果很奇怪。 在页面上,仅显示百分比条,但在小部件上,所有挑战都列为"待处理"

我的页面代码如下:

<?php
/**
* Template Name: Challenges Page
*/
?>
<?php get_header(); ?>
<!-- content -->
<div id="content">
    <?php query_posts( array( 'post_type' => 'challenges', 'posts_per_page' => -1 ) ); ?>
    <?php while (have_posts()) : the_post(); $more = 0; ?>
    <div class="post archive">
        <div class="post-comments"><?php comments_popup_link('0', '1', '%'); ?></div>
        <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
        <?php       
        $target = get_post_meta($post->ID, 'target', true);             
        $complete = get_post_meta($post->ID, 'complete', true);     
        $percentage = $complete / $target;
        $percentage = round($percentage * 100);
        $whatsleft = 100-$percentage;
        if($whatsleft < 0) $whatsleft=0;
        echo "<table width='250' border='0' cellpadding='0' cellspacing='0'><tr>";
        if($complete == $target) {
            echo "<td><img src='http://www.smokeyvstheworld.com/wp-content/uploads/2012/05/completed.gif' style='width:200>px;height:24px;'></td>";
        } else if ($complete == 0) {
            echo "<td><img src='http://www.smokeyvstheworld.com/wp-content/uploads/2012/05/pending.gif' style='width:200>px;height:24px;'></td>";
        } else {
            echo "<td><img src='http://www.smokeyvstheworld.com/wp-content/themes/spectre/images/brown/grnbar.jpg' style='width:". $percentage ."px;height:12px;'></td><td><img src='http://www.smokeyvstheworld.com/wp-content/themes/spectre/images/brown/grybar.jpg' style='width:". $whatsleft ."px;height:12px;'></td>";
        }
        echo "</tr><tr><td colspan='2'><div align='right'>". $complete ." of ". $target ." completed</div></td></tr></table>";                      
?>
<div class="post-date"><?php the_time('l, F jS, Y') ?></div>
<?php if (get_post_meta($post->ID, 'post_image_value', true)) { ?><div class="post-tnail"><a href="<?php the_permalink() ?>"><?php if (get_post_meta($post->ID, 'post_image_value', true) && $mb_resize == 0) { ?><img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "post_image_value", $single = true); ?>&amp;w=98&amp;h=98&amp;zc=1&amp;q=95" alt="<?php the_title(); ?>" /><?php } else if (get_post_meta($post->ID, 'post_image_value', true) && $mb_resize == 1) { ?><img src="<?php bloginfo('home'); ?><?php echo get_post_meta($post->ID, "post_image_value", $single = true); ?>" alt="<?php the_title(); ?>" /><?php } ?></a></div><?php } ?>
<?php the_excerpt() ?>
<p><a href="<?php the_permalink() ?>" class="more">Continue reading...</a></p>                      
                    </div>
<?php endwhile; ?>
</div>
<div id="sidebar"><?php get_sidebar(); ?></div>    
<?php get_footer(); ?>

我对我做错了什么感到困惑,以及为什么相同的代码在一个实例中有效而另一个实例无效......我尝试从括号切换到冒号,然后再切换回来等。

width:200>px;height:24px; => width:200px;height:24px;

尝试

       $target = (int) get_post_meta($post->ID, 'target', true);                    $complete = (int) get_post_meta($post->ID, 'complete', true);    

尝试将两个==都更改为===

if($complete === $target) {

} else if ($complete === 0) {

编辑:考虑一下,$complete值可能是一个字符串,所以如果上面不起作用,请将该行更改为

} else if ($complete === "0") {