I'm using a couple of custom wp_query loops on my page, the first one retrieves news from a certain category then displays a small excerpt from it with the permalink.
The second one is another wp_query that gets a custom post type with a few advanced custom fields.
The problem is I'd like another loop within the second one that gets 3 posts from the news section with a thumbnail (basically the opposite of the first loop, it'll get all the other categories). After reading countless articles on the loop I can't figure out how to make a 'nested' loop within my second loop. I'm sure it's got to be simple, seems like something that'd be easy to do.
Here's my code with a lot of html stripped out.
<?php
/*
* Template Name: Homepage
*/
?>
<?php get_header(); ?>
<div class="thenews">
<div class="newsinner">
<div class="grid-1">
<h6 class="nsix">latest news</h6>
</div> <!-- end div grid-1 -->
<div class="grid-2">
<?php
$recentPosts = new WP_Query();
$recentPosts->query('cat=5&showposts=1');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<p><?php echo ''.get_the_twitter_excerpt(); ?>...</p>
</div> <!-- end div grid-2 -->
<div class="grid-3">
<a href="<?php the_permalink() ?>">Read it</a>
<?php endwhile; wp_reset_query(); ?>
</div> <!-- end div grid-3 -->
</div> <!-- end div newsinner -->
</div> <!-- end div thenews -->
<div id="main-content">
<div class="typograhpy">
<div class="home-grid-1">
<div class="home-grid-1-inner">
<?php
$portfolio_query = new WP_Query(array(
'post_type' => 'headerhome',
'showposts' => 1
) );
?>
<?php while ($portfolio_query->have_posts()) : $portfolio_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<div class="anyres">
<p> <?php the_field('anyresolution'); ?></p>
</div> <!-- end div anyres -->
</div> <!-- end home-gird-1-inner -->
</div> <!-- end home-grid-1 -->
<div class="home-grid-2">
<div class="inner-home-1">
<div class="inside-home-1-1">
<h2><?php the_field('services_'); ?></h2>
<p> <?php the_field('services_text'); ?></p>
</div> <!-- end div inside-home-1-1 -->
<div class="inside-home-1-2">
<p> <?php the_field('services_text_right'); ?></p>
</div> <!-- end div inside-home-1-2 -->
</div> <!-- end div inner-home-1 -->
<div class="margindiv"></div>
<div class="inner-home-2">
<div class="brushpic"></div> <!-- end div brushpic -->
<h3><?php the_field('services_1_header') ?></h3>
<p><?php the_field('services_1_content'); ?></p>
</div><!-- end div inner-home-2 -->
<div class="inner-home-3">
<div class="cloudpic"></div> <!-- end div cloudpic -->
<h3><?php the_field('services_2_header') ?></h3>
<p><?php the_field('services_2_content'); ?></p>
</div> <!-- end div inner-home-3 -->
<div class="inner-home-4">
<div class="onetwoone"></div> <!-- end div onetwoone -->
<h3><?php the_field('services_3_header') ?></h3>
<p><?php the_field('services_3_content'); ?></p>
</div> <!-- end div inner-home-4 -->
</div> <!-- end div home-grid-2 -->
<div style="clear:both"></div>
</div> <!-- end div typograhpy -->
<div class="graphgrid">
</div> <!-- end div graphgrid -->
<div class="sizesdes">
<blockquote><?php the_field('gallery_blockquote_') ?></blockquote>
<p><?php the_field('gallery_content'); ?></p>
<?php endwhile; wp_reset_query(); ?>
</div>
</div><!-- end div main-content -->
<?php get_footer(); ?>
Working code, though it won't retrieve the_post_thumbnail
<?php
global $post;$backup=$post;
$inner_query = new WP_Query();
$inner_query->query('showposts=3');
?>
<?php while ($inner_query->have_posts()) : $inner_query->the_post(); $post=$backup; ?>
<?php the_post_thumbnail(); ?>
<p><?php echo ''.get_the_custom_excerpt(); ?>...</p>
<a href="<?php the_permalink() ?>">Read it</a>
<?php endwhile; ?>
$that_query->has_posts()
and->the_post()
, but make sure you are not using any of the data from the second loop after the third is started. Also make sure to usewp_query_reset()
after your while loop.