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 trial

WordPress

How 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
Andrew Shook
31,709 Points

Arash, 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>

UPDATED 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.

  1. I wanted each slide to have 8 videos (TWO ROWS) but right now it's only showing 4 videos (ONE ROW). How can I fix this to have 2 rows with 8 videos on each slide? Is it possible? Is it related to my CSS or something else?
  2. The slideshow is still showing 8 videos and not 10 (I'VE ADDED 10 VIDEOS TO THIS CATEGORY.) So right now, instead of 2 slides with 4 videos in each, I should have 2 slides in which the first slide will have 8 videos (in two rows) and the second one will have 2 videos. Where is the problem coming from? I have no clue! Thanks in advance

My UPDATED FlexSlider CSS:

/*
 * jQuery FlexSlider v2.2.0
 * http://www.woothemes.com/flexslider/
 *
 * Copyright 2012 WooThemes
 * Free to use under the GPLv2 license.
 * http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Contributing author: Tyler Smith (@mbmufffin)
 */


/* Browser Resets
*********************************/
.flex-container a:active,
.flexslider a:active,
.flex-container a:focus,
.flexslider a:focus  {outline: none;}
.slides,
.flex-control-nav,
.flex-direction-nav {margin: 0; padding: 0; list-style: none;}

/* Icon Fonts
*********************************/
/* Font-face Icons */
@font-face {
    font-family: 'flexslider-icon';
    src:url('fonts/flexslider-icon.eot');
    src:url('fonts/flexslider-icon.eot?#iefix') format('embedded-opentype'),
        url('fonts/flexslider-icon.woff') format('woff'),
        url('fonts/flexslider-icon.ttf') format('truetype'),
        url('fonts/flexslider-icon.svg#flexslider-icon') format('svg');
    font-weight: normal;
    font-style: normal;
}

/* FlexSlider Necessary Styles
*********************************/
.flexslider {margin: 0; padding: 0;}
.flexslider .slides > li {display: none; -webkit-backface-visibility: hidden;} /* Hide the slides before the JS is loaded. Avoids image jumping */
.flexslider .slides img {width: 100%; display: block;}
.flex-pauseplay span {text-transform: capitalize;}

/* Clearfix for the .slides element */
.slides:after {content: "\0020"; display: block; clear: both; visibility: hidden; line-height: 0; height: 0;}
html[xmlns] .slides {display: block;}
* html .slides {height: 1%;}

/* No JavaScript Fallback */
/* If you are not using another script, such as Modernizr, make sure you
 * include js that eliminates this class on page load */
.no-js .slides > li:first-child {display: block;}

/* FlexSlider Default Theme
*********************************/
.flexslider { margin: 0 0 60px; background: none; border: none; position: relative; -webkit-border-radius: 4px; -moz-border-radius: 4px; -o-border-radius: 4px; border-radius: 4px; -webkit-box-shadow: 0 1px 4px rgba(0,0,0,.2); -moz-box-shadow: 0 1px 4px rgba(0,0,0,.2); -o-box-shadow: 0 1px 4px rgba(0,0,0,.2); box-shadow: 0 1px 4px rgba(0,0,0,.2); zoom: 1; }
.flex-viewport { max-height: 2000px; -webkit-transition: all 1s ease; -moz-transition: all 1s ease; -o-transition: all 1s ease; transition: all 1s ease; }
.loading .flex-viewport { max-height: 300px; }
.flexslider .slides { zoom: 1; }
.carousel li { margin-right: 5px; }

/* Direction Nav */
.flex-direction-nav {*height: 0;}
.flex-direction-nav a  { text-decoration:none; display: block; width: 40px; height: 40px; margin: -20px 0 0; position: absolute; top: 50%; z-index: 10; overflow: hidden; opacity: 0; cursor: pointer; color: rgba(0,0,0,0.8); text-shadow: 1px 1px 0 rgba(255,255,255,0.3); -webkit-transition: all .3s ease; -moz-transition: all .3s ease; transition: all .3s ease; }
.flex-direction-nav .flex-prev { left: -50px; }
.flex-direction-nav .flex-next { right: -50px; text-align: right; }
.flexslider:hover .flex-prev { opacity: 0.7; left: 10px; }
.flexslider:hover .flex-next { opacity: 0.7; right: 10px; }
.flexslider:hover .flex-next:hover, .flexslider:hover .flex-prev:hover { opacity: 1; }
.flex-direction-nav .flex-disabled { opacity: 0!important; filter:alpha(opacity=0); cursor: default; }
.flex-direction-nav a:before  { font-family: "flexslider-icon"; font-size: 40px; display: inline-block; content: '\f001'; opacity: 0; }
.flex-direction-nav a.flex-next:before  { content: '\f002'; }

/* Pause/Play */
.flex-pauseplay a { display: block; width: 20px; height: 20px; position: absolute; bottom: 5px; left: 10px; opacity: 0.8; z-index: 10; overflow: hidden; cursor: pointer; color: #000; }
.flex-pauseplay a:before  { font-family: "flexslider-icon"; font-size: 20px; display: inline-block; content: '\f004'; }
.flex-pauseplay a:hover  { opacity: 1; }
.flex-pauseplay a.flex-play:before { content: '\f003'; }

/* Control Nav */
.flex-control-nav {width: 100%; position: absolute; bottom: -40px; text-align: center;}
.flex-control-nav li {margin: 0 6px; display: inline-block; zoom: 1; *display: inline;}
.flex-control-paging li a {width: 11px; height: 11px; display: block; background: #666; background: rgba(0,0,0,0.5); cursor: pointer; text-indent: -9999px; -webkit-border-radius: 20px; -moz-border-radius: 20px; -o-border-radius: 20px; border-radius: 20px; -webkit-box-shadow: inset 0 0 3px rgba(0,0,0,0.3); -moz-box-shadow: inset 0 0 3px rgba(0,0,0,0.3); -o-box-shadow: inset 0 0 3px rgba(0,0,0,0.3); box-shadow: inset 0 0 3px rgba(0,0,0,0.3); }
.flex-control-paging li a:hover { background: #333; background: rgba(0,0,0,0.7); }
.flex-control-paging li a.flex-active { background: #000; background: rgba(0,0,0,0.9); cursor: default; }

.flex-control-thumbs {margin: 5px 0 0; position: static; overflow: hidden;}
.flex-control-thumbs li {width: 25%; float: left; margin: 0;}
.flex-control-thumbs img {width: 100%; display: block; opacity: .7; cursor: pointer;}
.flex-control-thumbs img:hover {opacity: 1;}
.flex-control-thumbs .flex-active {opacity: 1; cursor: default;}

@media screen and (max-width: 860px) {
  .flex-direction-nav .flex-prev { opacity: 1; left: 10px;}
  .flex-direction-nav .flex-next { opacity: 1; right: 10px;}
}
.flex-prev{
    background-image: url(../images/prev.png);
    background-repeat: no-repeat;
    background-position: center;
    border: none;
}
.flex-next{
    background-image: url(../images/next.png);
    background-repeat: no-repeat;
    background-position: center;
    border: none;
}

.slides li{
    width: 210px!important;
    height: 200px;
    margin-left: 12px;
    margin-right:12px;
}
#fr{
    width: 210px;
    height: 149px;
}
.fr{
    width: 210px;
    height: 149px;
}
#fr a img{
    width: 210px;
}

#cap {
    width: 210px;
}
body{
    overflow-x:hidden;
}
html{
    overflow-x:hidden;
}

My UPDATED Theme.JS File:

(function($) {
    function make2Rows(iWidth) {
    var iHeight = parseFloat($('.flexslider .slides > li').height());
    $('.alliance-list .slides > li').css('width', iWidth+'px');
    $('.alliance-list .slides > li:nth-child(even)').css('margin', iHeight+'px 0px 0px -'+iWidth+'px');
}

$(window).load(function() {
    var itemCnt = 4; // this will be the number of columns per row
    var iWidth = parseFloat($('.flexslider').width() / itemCnt);
    $('.flexslider').flexslider({
        animation: "slide",
        slideshowSpeed: 6000,
        animationSpeed: 2500,
        animationLoop: true,
        directionNav: true,
        slideshow: true,
        touch: true,
        itemWidth: iWidth,
        minItems: itemCnt,
        maxItems: itemCnt,
        start: make2Rows(iWidth)
    });
});
})(jQuery);
Erik McClintock
Erik McClintock
45,783 Points

Arash,

To create a list item dynamically for each element in a given array, you'll want to use PHP's handy foreach loop.

Erik