【发布时间】:2017-04-16 03:29:27
【问题描述】:
我的网站上有 2 个部分:第一个是热门帖子(基于视图),第二个是最近的帖子。
如果帖子已经在热门帖子部分,我不希望它显示在“最近的帖子”部分。下面是我的代码。在第一个循环中,我创建了一个数组来存储该部分中的所有帖子 ID。在第二个循环中,我检查 id 是否在该数组中(可能不是最佳解决方案)。
由于某种原因,它只适用于第一个副本,即使$cont 变为true 所需的次数(我检查了echo)。那么是什么给了?
<?php
$popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );
$counter=0;
$post_ids = array();
while ( $popularpost->have_posts() ) : $popularpost->the_post();
$postID = get_the_ID();
$post_ids[$counter] = $postID;
?>
<a href="<?php the_permalink(); ?>" class="" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php $counter++; ?>
<?php endwhile; ?>
<?php $myquery = new WP_Query('posts_per_page=6');
while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<?php $post_id = get_the_ID(); ?>
<?php $post_ids_length = count($post_ids); ?>
<?php for ($i=0; $i < $post_ids_length; $i++) {
if ($post_id == $post_ids[$i]) {
$cont = "true";
} else {
$cont = "false";
}
} ?>
<?php if ($cont == "true") {
continue;
} ?>
<a href="<?php the_permalink(); ?>" class=""><?php the_title(); ?></a>
<?php endwhile; ?>
【问题讨论】: