编辑--我已经尝试过wp_reset_query() --它不起作用,第一个循环按预期执行,但是第二个循环只是跳转到else,我得到not working,我在一个页面中使用两个自定义的WP_Query循环,第一个是从某个类别获取帖子,第二个是按日期得到帖子,
这是密码
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$wpdb = new WP_Query($args);
if ($wpdb->have_posts()):
while ($wpdb->have_posts()):
$wpdb->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>第二回路
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4);
$wpdb = new WP_Query($args);
if ($wpdb->have_posts()):
while ($wpdb->have_posts()):
$wpdb->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo 'https://lamasec.pythonanywhere.com/static/img/vulnhub.png';endif; ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
else: echo 'not working';
endif;
?>我正在使用wp_reset_postdata();,但它似乎不起作用。
发布于 2018-12-24 13:12:42
在阅读了一个页面这里中的多个循环的文档之后,对于这两个循环,我只有一个WP_Query。我将所有帖子的ID存储在所需的类别中,并在第二个循环中检查它们,并对它们进行continue检查。这是最后的代码-
第一回路
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$wpdb = new WP_Query($args);
if ($wpdb->have_posts()):
while ($wpdb->have_posts()):
$wpdb->the_post();
$do_not_duplicate[] = $post->ID; ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php endwhile; ?>第二回路
if (have_posts()):
while (have_posts()):
the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
endif;
endif;
?>发布于 2018-12-24 07:51:28
您还应该使用:wp_reset_query()
https://stackoverflow.com/questions/53910555
复制相似问题