0

I succeeded in getting both images and video to echo into a bootstrap carousel. However, I run into a problem when the post contains no images. If there are no images then there is no class="item active" so the bootstrap carousel won't show.

Here is my workaround I feel like I'm almost there, can someone give me a hand?

<?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 {}
                }   
        }?>

Got it finally:

    <?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 {}
        }   
}?>
2
  • This seems to be a pure PHP question more appropriate for stackexchange.
    – mrwweb
    Mar 5, 2013 at 22:08
  • I asked on stackexchange as well. No one seems to be in a helping mood today.
    – popshuvit
    Mar 5, 2013 at 22:10

1 Answer 1

0

You might try to replace the foreach( $myCarousel as $carousel ) with:

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>";
            }
        }
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.