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 WordPress Theme Development The WordPress Loop Adding the Loop to the index.php File

Diego Villaseñor
Diego Villaseñor
12,615 Points

Using the loop to display latest posts as list items.

Hi,

so I want to have a home page that displays, as <li> the latests posts and events published somewhere else on the site. It should look something like this:

http://www.mediafire.com/view/8ez5ncpoi6dzetu/li.png

My html looks like this:

<div class="news-events">
    <ul>
        <li><img src="http://lorempixel.com/400/201" /><p>Imágen con descripción y link a artículo, noticia o evento</p></li>
        <li><img src="http://lorempixel.com/400/202" /><p>Imágen con descripción y link a artículo, noticia o evento</p></li>
        <li><img src="http://lorempixel.com/400/203" /><p>Imágen con descripción y link a artículo, noticia o evento</p></li>
        <li><img src="http://lorempixel.com/400/204" /><p>Imágen con descripción y link a artículo, noticia o evento</p></li>
        <li><img src="http://lorempixel.com/400/205" /><p>Imágen con descripción y link a artículo, noticia o evento</p></li>
        <li><img src="http://lorempixel.com/400/206" /><p>Imágen con descripción y link a artículo, noticia o evento</p></li>
    </ul>
</div>

So where should I insert the loop?

Also, how could I limit the number of items shown to only the latest 6 items?

Thanks a lot, in advance!

3 Answers

Andres Altuve
Andres Altuve
16,274 Points

Hey Diego,

In order to achieve what you are trying to do you have to use a custom loop that should look something like this:

Change YOURCATEGORY for the name of the category you want to use. Lets say you want to use news, YOURCATEGORY name should be news.

 ```html
        <?php query_posts( 'category_name=YOURCATEGORY&posts_per_page=6' ); ?>

            <?php while ( have_posts() ) : the_post(); ?>
                                   <li>
                                       <?php the_post_thumbnail(); ?>

                                               <h1><?php the_title(); ?></h1>
                                    <?php get_excerpt(); ?>
                                   </li>

            <?php endwhile; ?>

            <?php wp_reset_query(); ?>
        ```

the_post_thumbnail functions allows you to use featured images, in order to active it go to your functions.php add the following function :

add_theme_support( 'post-thumbnails');

Hope it helps.

Diego Villaseñor
Diego Villaseñor
12,615 Points

Thank you very much Andrés,

It was very helpful. My only comment is that in the excerpt function, I think it should have been the_excerpt();, rather than get_excerpt();. I was getting a very weird bug with that.

But maybe I´m mistaken, being so new at this.

Anyways thanks again!

Andres Altuve
Andres Altuve
16,274 Points

Hey Diego,

My mistake, you should use the_excerpt();