以下是我的search.php:
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type == 'page') : ?>
Show Page results
<?php endif; ?>
<?php endwhile; ?>
<?php rewind_posts(); ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type != 'page') : ?>
Show non-page results
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
No Results
<?php endif; ?>
该代码允许将帖子下的搜索结果与页面中的搜索结果分开。如果找不到帖子或页面的搜索结果,则会显示“无结果”文本。
然而,当找到页面但不是帖子的结果(反之亦然)时,在“发布搜索结果”下没有显示任何内容。我希望代码经过调整,以便在“帖子搜索”下找不到结果结果“但在”页面搜索结果“下找到了结果,”搜索结果“标题下方显示”无结果“文本。
非常感谢大家。
答案 0 :(得分:1)
尝试一下,您可以展开数组以包含您的网站正在使用的任何post_type:
<?php
foreach (array('post','page') as $pt) :
$search_query = new WP_Query(array(
'post_type' => $pt,
's' => $s,
'posts_per_page' => 10,
'paged' => $paged
)
);
?>
<?php if ($pt == 'post') : ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php else : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php endif; ?>
<?php
if ($search_query->have_posts()) : while ($search_query->have_posts()) : $search_query->the_post();
if ($pt == 'post') :
?>
Post Results Code
<?php else : ?>
Page Results Code
<?php endif; ?>
<?php endwhile; else : ?>
No Results
<?php endif; ?>
<?php endforeach; ?>