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 to Build a WordPress Theme Extending WordPress Template Functionality Adding Content in Custom Places: Part 2

Sandra Mazzocato
seal-mask
.a{fill-rule:evenodd;}techdegree
Sandra Mazzocato
Front End Web Development Techdegree Student 8,260 Points

Can`t make more than one loop work in one template

Hello

I need to create at least 3 loops in one template with post types, the first one is working perfectly, but when I place a second in the same template it doesn`t work correctly.

  • It should show 2 posts and shows only one;
  • Doesnยดt print the information it should (and the first one does and I`m using the same code for both);
  • Also kills the <?php get_footer() ?> and if I try to get a sidebar;

I`v tried to separate the code in a Get_template_part.. and the problem persists.

This is the code I'm using for both loops changing only funcion names and args

<?php

$args = array(
'post_type' => 'noticias',
'posts_per_page' => 6

);

$noticias = new WP_Query ( $args );

?>

<div class="row">

<?php if (have_posts() ) : while( $noticias->have_posts()) : $noticias->the_post(); ?>

<div class="small-12 large-4 columns">
<div class="panel noticia-home-item">
<div class="thumbtitle group">

                                <p><?php the_field( 'noticias' ); ?></p>
<div class="thumbnail thumb-noticias-home">

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
</div>

<h5>

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

<?php the_title(); ?>
</a>
</h5>

<p class="data">Publicado em <?php $data = DateTime::createFromFormat('Ymd',get_field('data'));
echo $data->format('d/m/y'); ?></p>

<div class="resumo"><?php the_excerpt(); ?> </div>
<div class="saiba-mais"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Saiba mais..</a></div>

</div><!--thumb title-->
</div><!--panel-->
</div><!--large-4-->

<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>

</div><!--row-notรญcias-->

The url is this - http://www.ufrgs.br/tsrs/telessauders/

1 Answer

Roberto Alicata
PLUS
Roberto Alicata
Courses Plus Student 39,959 Points

Try to reset the global $wp_query using wp_reset_query().

In the main loop you tested have_posts() instead you need to test if $noticias->have_posts()