0

Using the answer here: https://wordpress.stackexchange.com/a/207601/45709 exact works as it is intended. It returns 3 slides because only three slides exist. I had to slightly modify the code because the for loop is necessary inside the HTML structure as shown below. Unfortunately, after modifying the code, setting a max slides = 10 will return 10 slides despite only 3 existing. For the remaining 7, it prints them as empty data.

What I did to modify the code is use an initial check, if it passes, then I print the opening container HTML. Then inside that I added the for loop. I'm sure there is just a simple bug mistake there.

What is the mistake I've made with this code?

<?php
    //Check for slider
    $slideCheck = get_post_meta($post->ID, 'slide1', $single = true);
    // if there's a slide
    if($slideCheck) { ?>
        <section class="carousel slide" data-ride="carousel">
            <div class="carousel-inner">
                <?php
                    $max_slides = 10;
                    for ($i=1; $i<=$max_slides; $i++){
                        $key = 'slide' . $i;
                        $slide = get_post_meta($post->ID, $key, $single = true);
                        // check for slide text
                        $slide_text = get_post_meta($post->ID, $key . '_label', $single = true);
                    ?>
                    <div class="item active">
                        <img src="<?php echo $slide; ?>" class="slider-images" />
                        <div class="carousel-caption">
                            <h2><?php if($slide_text) { echo $slide_text; } else { echo the_title(); } ?></h2>
                        </div>
                    </div>
                <?php } ?>
            </div>
        </section>
    <?php } // end if statement
?>
  • 1
    you should check if $slide is not empty before outputting the html – czerspalace Nov 4 '15 at 19:33
  • Yeah that's it. I knew it was something dumb. I moved the if above but didn't keep it below as well. Thanks. – o_O Nov 4 '15 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.