我成功地将图像和视频都回送到引导旋转木马中。然而,当帖子中没有图片时,我遇到了一个问题。如果没有图像,则没有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 {}
}
}?>
最合适的回答,由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>";
}
}
}
}