如果另一个字段为空,如何回显另一个字段?

时间:2013-03-05 作者:popshuvit

我成功地将图像和视频都回送到引导旋转木马中。然而,当帖子中没有图片时,我遇到了一个问题。如果没有图像,则没有class=“item active”,因此引导转盘将不会显示。

这是我的解决方法,我感觉我就快到了,有人能帮我一下吗?

<?php $myCarousel = get_order_field(\'projectdetails_image\'); // use the Custom Group name 
        if( !empty( $myCarousel )) {
                foreach($myCarousel as $carousel){
                            if ($counter == 0) {
                                echo "<div class=\'item active\'>";
                                echo \'<img src="\';
                                echo get(\'projectdetails_image\',1,$carousel);
                                echo \'"/>\';
                                echo "</div>";
                            }
                            else{
                                echo "<div class=\'item\'>";
                                echo \'<img src="\';
                                echo get(\'projectdetails_image\',1,$carousel);
                                echo \'"/>\';
                                echo "</div>";
                            }
                            $counter++;

                $videos = get_order_field(\'video_vimeo\');
                foreach($videos as $video){
                    if ( get(\'video_vimeo\',TRUE) ) {
                            echo "<div class=\'item black\'><div id=\'video-wrap\'>";
                            echo get(\'video_vimeo\',1,$video);
                            echo "</div></div>";
                    }
                    else {}
                }
                }   
        }
        else{            
                $videos = get_order_field(\'video_vimeo\');
                foreach($videos as $video){
                    if ( get(\'video_vimeo\',TRUE) ) {
                            echo "<div class=\'item active black\'><div id=\'video-wrap\'>";
                            echo get(\'video_vimeo\',1,$video);
                            echo "</div></div>";
                    }
                    else {}
                }   
        }?>
终于明白了:

    <?php $myCarousel = get_order_field(\'projectdetails_image\'); // use the Custom Group name 
if( !empty( $myCarousel )) {
        foreach($myCarousel as $carousel){
        $myimg = get(\'projectdetails_image\',1,$carousel);
        if ( !$myimg ) {
        $videos = get_order_field(\'video_vimeo\');
        foreach($videos as $video){
            if ( get(\'video_vimeo\',TRUE) ) {
                    echo "<div class=\'item active black\'><div id=\'video-wrap\'>";
                    echo get(\'video_vimeo\',1,$video);
                    echo "</div></div>";
            }
            else {}
        }   
        }
        if ( $myimg ) {
            echo "<div class=\'item".( $counter == 0 ? \' active\' : \'\' )."\'>";
            echo \'<img src="\';
            echo $myimg;
            echo \'"/>\';
            echo "</div>";
            $counter++;

            $videos = get_order_field(\'video_vimeo\');
            foreach($videos as $video){
            if ( get(\'video_vimeo\',TRUE) ) {
                echo "<div class=\'item black\'><div id=\'video-wrap\'>";
                echo get(\'video_vimeo\',1,$video);
                echo "</div></div>";
            }}
        }
    }
}
else{            
        $videos = get_order_field(\'video_vimeo\');
        foreach($videos as $video){
            if ( get(\'video_vimeo\',TRUE) ) {
                    echo "<div class=\'item active black\'><div id=\'video-wrap\'>";
                    echo get(\'video_vimeo\',1,$video);
                    echo "</div></div>";
            }
            else {}
        }   
}?>

1 个回复
最合适的回答,由SO网友:Vivienne 整理而成

您可以尝试将foreach($myCarousel as$carousel)替换为:

foreach($myCarousel as $carousel){
    $myimg = get(\'projectdetails_image\',1,$carousel);
    if ( !$myimg ) {
        // use this if you want to show a default image when no image is available in the post
        $myimg = get_template_directory_uri().\'/images/default_banner.jpg\';
    }
    if ( $myimg ) {
        echo "<div class=\'item".( $counter == 0 ? \' active\' : \'\' )."\'>";
        echo \'<img src="\';
        echo $myimg;
        echo \'"/>\';
        echo "</div>";
        $counter++;

        $videos = get_order_field(\'video_vimeo\');
        foreach($videos as $video){
            if ( get(\'video_vimeo\',TRUE) ) {
                echo "<div class=\'item black\'><div id=\'video-wrap\'>";
                echo get(\'video_vimeo\',1,$video);
                echo "</div></div>";
            }
        }
    }
}

结束

相关推荐

注意:未定义的索引:在Options-Framework.php中

我在选项框架中生成以下通知。php。注意:未定义索引:在选项框架中。php我认为这与卫生处理有关,但不知道如何解决这个问题。我在选项中定义了以下数组。php// Pull all the custom taxonomies into an array $options_password_taxonomies = array(); $taxonomies_password_terms_obj = get_terms(\'password_gallery_category\'); fo