0

I'm trying to create a bootstrap carousel with the featured image as a background in a div. It works for the first post but the second post isn't loading the right background (it loads the one from the first post).

Code:

<div class="jumbotron jumbotron-header grey-dark">

<div id="myCarousel2" class="carousel slide" data-ride="carousel" data-interval="false">

<div class="carousel-inner" role="listbox">




<?php
$queryObject = new  Wp_Query( array(
    'showposts' => 5,
    'post_type' => array('slider'),
    'orderby' => 1
    ));

// The Loop
if ( $queryObject->have_posts() ) :
    $i = 0;
    while ( $queryObject->have_posts() ) :
        $queryObject->the_post();
        if ( $i == 0 ) : ?>

        <?php global $post; ?>
        <?php
        $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
        ?>

        <div class="item jumbotron-content active" style="background-image:url('<?php echo $src[0]; ?>');width: 100%;height: 500px;display: cover;background-position: center center; background-repeat: no-repeat;background-size: cover;">


            <div class="container">


                  <h2><?php the_title();?><h2>

                  </div>



        </div>


        <?php endif;
        if ( $i != 0 ) : ?>
        <div class="item jumbotron-content" style="background-image:url('<?php echo $src[0]; ?>');width: 100%;height: 500px;display: cover;background-position: center center; background-repeat: no-repeat;background-size: cover;">



            <div class="container">

                  <h2><?php the_title();?><h2>

            </div>


        </div>
        <?php endif; ?>

        <?php $i++;
    endwhile;
endif;
  ?>

Anyone know what I'm doing wrong? Thanks in advance!

  • What are you getting for the value of $src[0] when $i != 0? You know $i will either = 0 or not so an elseif makes a bit more sense there. – jdm2112 Oct 19 '15 at 21:17
  • $i != 0 isn't giving me the featured image URL but does give me the title o the second post.. – rwzdoorn Oct 20 '15 at 7:55
0

Problem solved:

Had to add the same global post after the endif:

        <?php endif;
        if ( $i != 0 ) : ?>            

<?php global $post; ?>
        <?php
        $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
        ?>

Your Answer

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

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