admin管理员组文章数量:1130349
My site has 3 unique post types:
- Default Blog posts ("post")
- custom type "lesson"
- custom type "series"
When the user searches the site, I would like pertinent results from all 3 post types to show up on the search results page. The "posts" results are in one container, "lesson" results in a separate container, etc. How can I modify the search page to accomplish this?
Here is my current loop:
<?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(); ?>
My site has 3 unique post types:
- Default Blog posts ("post")
- custom type "lesson"
- custom type "series"
When the user searches the site, I would like pertinent results from all 3 post types to show up on the search results page. The "posts" results are in one container, "lesson" results in a separate container, etc. How can I modify the search page to accomplish this?
Here is my current loop:
<?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(); ?>
Share
Improve this question
asked Mar 20, 2015 at 16:08
tdctdc
2711 gold badge3 silver badges12 bronze badges
2
|
1 Answer
Reset to default 21You can run the same loop multiple times by using rewind_posts() to output each type separately.
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;
}
}
My site has 3 unique post types:
- Default Blog posts ("post")
- custom type "lesson"
- custom type "series"
When the user searches the site, I would like pertinent results from all 3 post types to show up on the search results page. The "posts" results are in one container, "lesson" results in a separate container, etc. How can I modify the search page to accomplish this?
Here is my current loop:
<?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(); ?>
My site has 3 unique post types:
- Default Blog posts ("post")
- custom type "lesson"
- custom type "series"
When the user searches the site, I would like pertinent results from all 3 post types to show up on the search results page. The "posts" results are in one container, "lesson" results in a separate container, etc. How can I modify the search page to accomplish this?
Here is my current loop:
<?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(); ?>
Share
Improve this question
asked Mar 20, 2015 at 16:08
tdctdc
2711 gold badge3 silver badges12 bronze badges
2
-
Don't use
elseif, use onlyif. This way, all the templates will be displayed. – Ciprian Commented Mar 20, 2015 at 16:52 -
I don't think
ifvs.elseifwill make a difference here. – s_ha_dum Commented Mar 25, 2015 at 16:04
1 Answer
Reset to default 21You can run the same loop multiple times by using rewind_posts() to output each type separately.
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;
}
}
本文标签: loopgroup search results by post type
版权声明:本文标题:loop - group search results by post type? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749045549a2307808.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


elseif, use onlyif. This way, all the templates will be displayed. – Ciprian Commented Mar 20, 2015 at 16:52ifvs.elseifwill make a difference here. – s_ha_dum Commented Mar 25, 2015 at 16:04