我尝试按2个标签中的类别分隔搜索结果。除了显示“无结果”消息外,一切正常。仅当两个类别均未找到时,才会显示此消息。但是当一个选项卡有结果而另一个没有结果时 - 没有显示任何内容。
我正在寻找为每个标签显示“无结果”的方法。我的意思是,如果在猫1中没有发现任何东西,并且在猫2中发现了一些结果 - >在标签1中显示“无结果”,并在标签2中显示结果。
有什么建议吗?
代码在这里:
<div id="tab-content1" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 3,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--text">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?> </a></span>
<span> </span>
</span>
</div>
<div class="post-item--thumbnail">
<div class="post-thumbnail">
<div class="backgroundImage_1hK9M post-thumbnail--image" style="background-image: url('<?php echo $url; ?>');"></div>
<span></span>
<span></span>
</div>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content1 -->
<div id="tab-content2" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 4,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--circle">
<?php the_field('digest_number'); ?>
</div>
<div class="post-item--text no-margin">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><a href="<?php the_permalink(); ?>"><p><?php the_field('short_description'); ?></p> </a></span>
<span> </span>
</span>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content2 -->
</div>
答案 0 :(得分:0)
您可以在每个循环上使用if (have_posts()) :
来确定是否显示...
if (have_posts()) :
//while loop
else:
echo '<h1>no posts found</h1>';
endif;