Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer

up vote 0 down vote accepted

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>";
            }
        }
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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