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 am trying to setup a wordpress slider, how can I achieve exact below result within a wordpress loop

            <div id="carousel">
                <img src="slide1.png" alt="" class="slide" />
                <img src="slide2.png" alt="" class="slide" />
                <img src="slide3.png" alt="" class="slide" />
            </div>

            <ul id="carousel-descriptions">
                <li class="desc current">Slide 1 Description Goes Here</li>
                <li class="desc">Slide 2 Description Goes Here</li>
                <li class="desc">Slide 3 Description Goes Here</li>
            </ul>

            <div id="carousel-controls">
                <span class="control current">Show Me<br>Slide 1</span>
                <span class="control">Show Me<br>Slide 2</span>
                <span class="control">Show Me<br>Slide 3</span>
            </div>

for example, I want to show above code within

 $my_query = new WP_Query('cat=49&showposts=4&orderby=RAND');
while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID;
?>

 above code

<?php endwhile; ?>

Please do let me know if there is any other way to achieve the same result but orderby=RAND is important for me

BR

share|improve this question
    
is the css class .current on the first post per section important? is the image the featured imge aka post thumbnail? –  Michael May 25 '12 at 22:09
    
yes, the css class is important to show the current item highlightes. images are post thumbnail –  user1305063 May 25 '12 at 22:11
add comment

2 Answers 2

up vote 2 down vote accepted

Use rewind_posts() to reset a loop and run it multiple times:

<?php

$my_query = new WP_Query('cat=49&showposts=4&orderby=RAND');
?>
<div id="carousel">
<?php
while ($my_query->have_posts()) : $my_query->the_post();
    // output each slide
endwhile;
?>
</div>
<?php

$my_query->rewind_posts();
?>
<ul id="carousel-descriptions">
<?php
while ($my_query->have_posts()) : $my_query->the_post();
    // output each desc
endwhile;
?>
</ul>
<?php

$my_query->rewind_posts();
// etc.
share|improve this answer
    
perfect, thanks –  user1305063 May 25 '12 at 22:16
add comment

Or save the results into variables and run loop once like :

$carousel_descriptions .= 'Slide 1 Description Goes Here';

then after the loop its finished just echo those variables.

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.