对于一个WordPress网站,我有几个自定义帖子类型,名为“城市”和“书籍”,以及常规博客帖子。我已自定义搜索结果(仅在帖子标题中搜索)页面,以便按自定义帖子类型排序结果。但问题是自定义帖子类型中的搜索结果并未真正分组。示例:当我搜索字母'am'时,我会在搜索结果页面中获得以下结果:
BOOKS
-----
Book 1
CITIES
------
City 1
BOOKS
-----
Book 2
Book 3
CITIES
------
City 2
City 3
City 4
BLOGPOSTS
---------
Post 1
Post 2
Post 3
etc.
我想要的是什么:
BOOKS
-----
Book 1
Book 2
Book 3
CITIES
------
City 1
City 2
City 3
City 4
BLOGPOSTS
---------
Post 1
Post 2
Post 3
etc.
这是我的循环:
<?php if(have_posts()) : ?>
<?php
$last_type = "";
$typecount = 0;
while ( have_posts() ) : the_post();
?>
<?php
if ($last_type != $post->post_type){
$typecount = $typecount + 1;
if ($typecount > 1){
echo '</div><!-- close type-container -->'; //close type container
}
// save the post type.
$last_type = $post->post_type;
//open type container
switch ($post->post_type) {
case 'books':
echo "<div class=\"books-container\"><h2>";
_e('Books', 'qode');
echo "</h2>";
break;
case 'cities':
echo "<div class=\"cities-container\"><h2>";
_e('Destinations', 'qode');
echo "</h2>";
break;
case 'post':
echo "<div class=\"blog-container\"><h2>";
_e('Blogposts', 'qode');
echo "</h2>";
break;
}
}
?>
<?php if('books' == get_post_type()) : ?>
<?php get_template_part('templates/books_search', 'loop'); ?>
<?php elseif('cities' == get_post_type()) : ?>
<?php get_template_part('templates/cities_search', 'loop'); ?>
<?php elseif('post' == get_post_type()) : ?>
<?php get_template_part('templates/blog_search', 'loop'); ?>
<?php endif; ?>
<?php endwhile; // endwhile have_posts ?>
<?php // Pagination ?>
<?php if($qode_options_proya['pagination'] != "0") : ?>
<?php pagination($wp_query->max_num_pages, $blog_page_range, $paged); ?>
<?php endif; ?>
<?php else: //If no posts are present ?>
<div class="entry nothing-found">
<h3><?php _e('Sorry, there is no 100% ', 'qode') . " " . the_search_query() . " " . _e(' yet.') ?></h3>
<p><?php _e('But there are loads of other 100% destinations available. Try again in the searchfield below.', 'qode'); ?>
</div>
<?php endif; // endif have_posts ?>
这是包含在内的CPT模板文件的示例:
<div <?php post_class('vc_span4 wpb_column column_container'); ?> style="padding-bottom: 60px;">
<div class="wpb_wrapper book">
<?php // Get the post thumbnail ?>
<div class="wpb_single_image wpb_content_element element_from_fade element_from_fade_on">
<div class="wpb_wrapper">
<?php
if ( has_post_thumbnail() ) {
echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . the_title_attribute( 'echo=0' ) . '" class="book-holder" ><span class="book-thumb">';
echo get_the_post_thumbnail( $post->ID, 'book-thumbnail' );
echo '</span></a>';
}
?>
</div>
</div>
</div>
<div class="wpb_text_column wpb_content_element text-align-center" style="margin: 20px 0px 0px 0px; ">
<div class="wpb_wrapper">
<h5><?php the_title(); ?></h5>
</div>
<a href="<?php the_permalink(); ?>" target="_blank" class="qbutton small" style="margin: 20px 0px 0px 0px; "><?php _e('More Info', 'qode'); ?></a>
</div>
</div>
我必须在这里做错事,但我无法弄清楚是什么。有人可以帮助我吗?