Reputation: 615
I have two loops running on my page one for getting one set of posts from a specific category and then further down getting posts from a custom post type but for some reason If I output both of them the second loop does not show and if I comment out the first the second one does show?
Im a bit confused as to why?
FIRST LOOP UPDATED
<?php
$post_query = new WP_Query(array( 'category_name' => 'email-content'));
if ( $post_query->have_posts() ) {
echo '<div class="tabs tabs_default">';
echo '<ul class="horizontal">';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo'<li><a href="#'.$title.'">'.get_the_title().'</a></li>';
}
echo '</ul>';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo '<div id="'.$title.'">';
echo '<div><button class="copy" id="'.$title.'" data-clipboard-text="'.get_the_content().'" title="Copy">Copy</button></div>';
echo '<div>'.get_the_content().'</div>';
echo '</div>';
}
$post_query->reset_postdata();
}
?>
SECOND LOOP UPDATED
I didn't need one of the loops so I refined the code a bit further but still I am not having any luck with the second loop outputting.
<?php
$featureThumb = new WP_Query( array(
'post_type' => 'resources',
'meta_key' => 'file_upload',
'posts_per_page' => -1
));
while ($featureThumb->have_posts()) : $featureThumb->the_post();
echo '<div>';
if (has_post_thumbnail($post->ID)) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'homepage-thumb-thumb' );
echo '<img src="' . $thumb[0] . '" width="200" height="200" />' ;
};
echo '<p><a href="'. get_field('file_upload') .'" target="_blank" download>Click here to download as PDF</a></p>';
endwhile;
unset($featureThumb);
}
?>
Upvotes: 2
Views: 69
Reputation: 11472
It seems that you have three queries and 4 loops, not two.
Also, you have an extra }
after the unset($featureThumb);
that you should remove.
The three queries you posted in your question are:
$post_query
, $loop
and $featureThumb
. At the last one your loop is incorect. It uses the object of the second one. Change at the third query in the loop, $loop
with $featureThumb
.
You need to use wp_reset_postdata()
after the first two queries instead of wp_reset_query()
as someone suggested and it should both work.
wp_reset_query()
ensures that the main query has been reset to the original main query, while on the other hand wp_reset_postdata()
makes sure that the global $post
has been restored to the current post in the main query.
UPDATE:
If that does not work, if global $post
object is not defined, try:
$post_query->reset_postdata();
$loop->reset_postdata();
UPDATE 2:
Your first query and loop should look like this:
<?php
$post_query = new WP_Query(array( 'category_name' => 'email-content'));
if ( $post_query->have_posts() ) {
echo '<div class="tabs tabs_default">';
echo '<ul class="horizontal">';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo'<li><a href="#'.$title.'">'.get_the_title().'</a></li>';
}
echo '</ul>';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo '<div id="'.$title.'">';
echo '<div><button class="copy" id="'.$title.'" data-clipboard-text="'.get_the_content().'" title="Copy">Copy</button></div>';
echo '<div>'.get_the_content().'</div>';
echo '</div>';
}
$post_query->reset_postdata();
}
?>
Upvotes: 1
Reputation: 341
You have to use wp_reset_query();
at the end of the first loop as well. So the first loop should be:
<?php
$post_query = new WP_Query(array( 'category_name' => 'email-content'));
if ( $post_query->have_posts() ) {
echo '<div class="tabs tabs_default">';
echo '<ul class="horizontal">';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo'<li><a href="#'.$title.'">'.get_the_title().'</a></li>';
}
echo '</ul>';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo '<div id="'.$title.'">';
echo '<div><button class="copy" id="'.$title.'" data-clipboard-text="'.get_the_content().'" title="Copy">Copy</button></div>';
echo '<div>'.get_the_content().'</div>';
echo '</div>';
}
wp_reset_query();
}
?>
Upvotes: 1