Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialArash Emadi
10,002 PointsHow should I write a loop for my <li> tags?
Hello everyone,
I'm getting closer and closer to getting my slideshow working perfectly. However, I don't know how to write a loop in which, I can produce more than one <li> tags for my unordered list. Right now my latest 8 Videos show up in ONE SLIDE and therefore there isn't any movement in my slideshow. There's just 1 slide with 8 of my latest videos. I thought the best way to get this working is to make the <li></li> dynamic with PHP but I don't know how to get it working. Can you help me out please? My updated page code is written below:
LINK: http://www.sama.tv/newsamatv/pearl
<center>
<ul class="slides">
<?php
$args = array(
'post_type' => 'pearl'
);
$slideshow_query = new WP_Query( $args );
?>
<?php echo '<li>';?>
<?php
if ( have_posts() ) : while ( $slideshow_query->have_posts() ) : $slideshow_query->the_post();
?>
<div id="fr"><a href="<?php the_permalink();?>"><img src="<?php the_field('image');?>"/><div id="cap"><?php the_title(); ?>
</div></a></div>
<?php endwhile;?>
<?php endif;?>
<?php echo '</li>';?>
</ul>
</center>
2 Answers
Andrew Shook
31,709 PointsArash, Erik is right that foreach loop is best, but WordPress has it's own loop so you are best off using that. I refactored you code a little bit so that each post would be wrapped in it's own <li> and the <ul> will only show up if there are posts.
<center>
<?php
$args = array(
'post_type' => 'pearl'
);
$slideshow_query = new WP_Query( $args );
?>
<?php
/* if ( have_posts() ) :
* don't use this with custom queries instead us this:
*/
if ($slideshow_query-> have_posts() ) :
/* when using a custom query this if statement should check the have_posts()
* of the custom query, If you don't the have_post will check the normal query
* and could return a false positive to the if statement.
*/
?>
<ul class="slides">
<?php while ( $slideshow_query->have_posts() ) : $slideshow_query->the_post(); ?>
<li>
<div id="fr">
<a href="<?php the_permalink();?>">
<img src="<?php the_field('image');?>"/>
<div id="cap"><?php the_title(); ?></div>
</a>
</div>
</li>
<?php endwhile;?>
</ul>
<?php endif;?>
</center>
Erik McClintock
45,783 PointsArash,
To create a list item dynamically for each element in a given array, you'll want to use PHP's handy foreach loop.
Erik
Arash Emadi
10,002 PointsArash Emadi
10,002 PointsUPDATED Hey Andrew,
Thanks for your great answer. Now the slideshow is showing up and working, but there are two issues with it than I need assistance with.
My UPDATED FlexSlider CSS:
My UPDATED Theme.JS File: