我的网站有3种独特的帖子类型:
当用户搜索网站时,我希望所有3种帖子类型的相关结果都显示在搜索结果页面上。“posts”结果在一个容器中,“lesson”结果在一个单独的容器中,等等。如何修改搜索页面来实现这一点?
这是我当前的循环:
<?php get_header(); ?>
<div class="row">
<div class="small-12 large-8 columns" role="main">
<?php do_action(\'foundationPress_before_content\'); ?>
<h2><?php _e(\'Search Results for\', \'FoundationPress\'); ?> "<?php echo get_search_query(); ?>"</h2>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if( get_post_type() == \'lesson\' ) {
get_template_part(\'content\', \'lesson\');
} else if ( get_post_type() == \'post\' ) {
get_template_part(\'content\', get_post_format());
}
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( \'content\', \'none\' ); ?>
<?php endif;?>
<?php do_action(\'foundationPress_before_pagination\'); ?>
<?php if ( function_exists(\'FoundationPress_pagination\') ) { FoundationPress_pagination(); } else if ( is_paged() ) { ?>
<nav id="post-nav">
<div class="post-previous"><?php next_posts_link( __( \'← Older posts\', \'FoundationPress\' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( \'Newer posts →\', \'FoundationPress\' ) ); ?></div>
</nav>
<?php } ?>
<?php do_action(\'foundationPress_after_content\'); ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
最合适的回答,由SO网友:Milo 整理而成
您可以使用rewind_posts()
分别输出每种类型。
if( have_posts() ){
$types = array(\'post\', \'lesson\', \'series\');
foreach( $types as $type ){
echo \'your container opens here for \' . $type;
while( have_posts() ){
the_post();
if( $type == get_post_type() ){
get_template_part(\'content\', $type);
}
}
rewind_posts();
echo \'your container closes here for \' . $type;
}
}