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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Diego Villaseñor](https://secure.gravatar.com/avatar/ea5f5760095daab92d6d232c85b09488?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Diego Villaseñor
12,615 PointsUsing 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](https://uploads.teamtreehouse.com/production/profile-photos/883122/micro_foto.jpg)
Andres Altuve
16,274 PointsHey 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](https://secure.gravatar.com/avatar/ea5f5760095daab92d6d232c85b09488?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Diego Villaseñor
12,615 PointsThank 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](https://uploads.teamtreehouse.com/production/profile-photos/883122/micro_foto.jpg)
Andres Altuve
16,274 PointsHey Diego,
My mistake, you should use the_excerpt();