0

I need to create a slider navigation which has one li for each post. I currently have this code:

<?php
$args = array( 'post_type' => 'slides', 'orderby' => 'menu_order');
$loop = new WP_Query( $args );
?>

<div id="myCarousel" class="carousel slide">
  <ol class="carousel-indicators">

    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <?php while ( $loop->have_posts() ) : $loop->the_post();  $x = 1 ?>
    <li data-target="#myCarousel" data-slide-to="<?php echo $x ?>"></li>
    <?php $x = $x + 1 ?>
    <?php endwhile; ?>

  </ol>

As I need the first one to stay active.. But this isn't quite working for me

7
  • I'm getting one extra and they all say 1 so the number isn't getting bigger each time Commented Sep 23, 2013 at 21:35
  • because $x = 1 is inside the loop Commented Sep 23, 2013 at 21:36
  • Why are you using $x = 1 Commented Sep 23, 2013 at 21:36
  • @Anagio maybe she needs an iterator number for the slides.. Commented Sep 23, 2013 at 21:37
  • I do need an iterator but it would probably be easier to loop through the post count? I just don't know how! Commented Sep 23, 2013 at 21:37

2 Answers 2

1

try this:

** take note that the $x variable was moved outside the loop so that your data-slide-to value will not all be equal to 1;

<div id="myCarousel" class="carousel slide">
    <ol class="carousel-indicators">

        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <?php $x = 1; ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <li data-target="#myCarousel" data-slide-to="<?php echo $x++; ?>"></li>
        <?php endwhile; ?>
    </ol>
</div>

** if you are getting extras, it maybe because you have put a static

<li data-target="#myCarousel" data-slide-to="0" class="active"></li>

inside the loop, so what you might want could be this instead:

<div id="myCarousel" class="carousel slide">
    <ol class="carousel-indicators">
        <?php $x = 0; ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <li data-target="#myCarousel" data-slide-to="<?php echo $x; ?>" <?php echo ($x++==0)?'class="active"':'';?>></li>
        <?php endwhile; ?>
    </ol>
</div>
Sign up to request clarification or add additional context in comments.

6 Comments

You should note that the primary difference here is that you are setting $x = 1 BEFORE the loops, whereas before his code was resetting $x = 1 which each iteration.
this works but I still have one extra, the li that isn't in the loop probably should be.. but I need it to have the class "active"
good thing i have edited the answer already.. i have just read your comment. pls tell me if it works now.
although you dont do anything on your loop.. you could put the_content() or something inside the <li></li> tags.. that's what you want to do i guess.. because the current code would generate empty <li>'s, if im not mistaken..
the code i gave will put the class="active" on the first element.. no need for extra variables.. ;)
|
0

You can get the count like this

$posts = new WP_Query( $postargs );
$postcount = $posts ->post_count;

Now do echo $postcount; and you will have the number of posts.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.