admin管理员组文章数量:1292618
I've been struggling to make it work. This code works and returns post_type title and post titles inside it. I found it here
But i need to have some sort of formatting and excerpt. Currently I cannot make it work with get_post_type_object . I'd very much like to use regular search loop with while and get custom templates for post types with get_template_part()
<?php
$types = array( 'product', 'post', 'page', 'useful' );
$posts_titles = [];
while ( have_posts() ) {
the_post();
$type = $post->post_type;
if ( !isset($posts_titles[$type]) )
$posts_titles[$type] = [];
$posts_titles[$type][] = get_the_title();
}
rewind_posts();
foreach( $types as $type ) :
if ( !isset($posts_titles[$type]) )
continue;
?>
<div class="search_products_wrap search-results-post-type-item post-type-<?php echo $type ?>">
<header class="post-type-header">
<div class="search-block-title">
<h2 class="post-type-title">
<?php
$post_type_obj = get_post_type_object( $type );
echo $post_type_obj->labels->name;
?>
</h2>
</div>
</header>
<div class="search-results-list">
<?php foreach( $posts_titles[$type] as $title ) : ?>
<div class="search-results-list-item">
<h3 class="entry-title"><?php echo htmlspecialchars($title); ?></h3>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach;
?>
This code kind of works but it lacks proper implementation. It returns post type titles when there are no results.
if( have_posts() ){
$types = array('product', 'post', 'page', 'useful');
foreach( $types as $type ){
echo '<div class="clearfix post-type-wrap '. $type .'">';
if ( $type == 'post' ) {
$blocktitle = __( 'Posts', 'theme' );
} elseif ( $type == 'page' ) {
$blocktitle = __( 'Pages', 'theme' );
} elseif ( $type == 'product' ) {
$blocktitle = __( 'Products', 'theme' );
} else {
$blocktitle = __( 'Else', 'theme' );
}
?>
<h3><?php echo $blocktitle; ?></h3>
<?php
while( have_posts() ){
the_post();
if( $type == get_post_type() ){
get_template_part( 'assets/search/content', $type );
}
}
rewind_posts();
echo '</div>';
}
}
Can someone guide me to the right direction how i can achieve it?
I've been struggling to make it work. This code works and returns post_type title and post titles inside it. I found it here
But i need to have some sort of formatting and excerpt. Currently I cannot make it work with get_post_type_object . I'd very much like to use regular search loop with while and get custom templates for post types with get_template_part()
<?php
$types = array( 'product', 'post', 'page', 'useful' );
$posts_titles = [];
while ( have_posts() ) {
the_post();
$type = $post->post_type;
if ( !isset($posts_titles[$type]) )
$posts_titles[$type] = [];
$posts_titles[$type][] = get_the_title();
}
rewind_posts();
foreach( $types as $type ) :
if ( !isset($posts_titles[$type]) )
continue;
?>
<div class="search_products_wrap search-results-post-type-item post-type-<?php echo $type ?>">
<header class="post-type-header">
<div class="search-block-title">
<h2 class="post-type-title">
<?php
$post_type_obj = get_post_type_object( $type );
echo $post_type_obj->labels->name;
?>
</h2>
</div>
</header>
<div class="search-results-list">
<?php foreach( $posts_titles[$type] as $title ) : ?>
<div class="search-results-list-item">
<h3 class="entry-title"><?php echo htmlspecialchars($title); ?></h3>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach;
?>
This code kind of works but it lacks proper implementation. It returns post type titles when there are no results.
if( have_posts() ){
$types = array('product', 'post', 'page', 'useful');
foreach( $types as $type ){
echo '<div class="clearfix post-type-wrap '. $type .'">';
if ( $type == 'post' ) {
$blocktitle = __( 'Posts', 'theme' );
} elseif ( $type == 'page' ) {
$blocktitle = __( 'Pages', 'theme' );
} elseif ( $type == 'product' ) {
$blocktitle = __( 'Products', 'theme' );
} else {
$blocktitle = __( 'Else', 'theme' );
}
?>
<h3><?php echo $blocktitle; ?></h3>
<?php
while( have_posts() ){
the_post();
if( $type == get_post_type() ){
get_template_part( 'assets/search/content', $type );
}
}
rewind_posts();
echo '</div>';
}
}
Can someone guide me to the right direction how i can achieve it?
Share Improve this question edited Sep 13, 2022 at 8:06 m2itz asked Sep 9, 2022 at 11:47 m2itzm2itz 112 bronze badges1 Answer
Reset to default 0Solution found by counting! It counts if there are any posts and if not then skips.
<?php if ( have_posts() ) : ?>
<?php
$types = array('product' => 0, 'post' => 0, 'page' => 0);
while ( have_posts() ) {
the_post();
if (array_key_exists(get_post_type(), $types)) {
$types[get_post_type()]++;
}
}
?>
<?php foreach ($types AS $type => $nb_of_type) : ?>
<?php if ($nb_of_type > 0) : ?>
<div class="post-type-wrap <?php echo $type; ?>">
<div class="search-block-title"><h2><?php echo $blocktitle; ?></h2></div>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( get_post_type() === $type ) : ?>
<?php get_template_part('assets/search/content', $type); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php else :
_e( 'Sorry, no posts were found.' );
?>
<?php endif; ?>
本文标签: theme developmentWordpress search results grouped by post type
版权声明:本文标题:theme development - Wordpress search results grouped by post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738423432a2085987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论