SearchWP Documentation

インストールガイドを表示したり、ナレッジベースを参照したり、SearchWPの多くのフックについて確認したりできます。

ライブ検索結果をカテゴリ別にグループ化

カスタム結果表示 SearchWPのライブ検索拡張機能は、あらゆる方法でカスタマイズを容易にするように設計されています。たとえば、結果を投稿カテゴリ別にグループ化したい場合は、これをカスタム結果テンプレートの出発点として使用できます。

<?php if ( ! have_posts() ) : ?>
<p>No results</p>
<?php return; endif;?>
<?php
global $post;
$grouped_results = [];
$no_term_results = [];
while ( have_posts() ) {
the_post();
$categories = get_the_terms( $post, 'category' );
// Group by the first Category returned.
if ( ! empty( $categories ) && isset( $categories[0] ) ) {
// If this group doesn't exist yet, create it.
if ( ! array_key_exists( $categories[0]->term_id, $grouped_results ) ) {
$grouped_results[ $categories[0]->term_id ] = [
'term' => $categories[0],
'results' => [],
];
}
// Add this result to the group.
$grouped_results[ $categories[0]->term_id ]['results'][ $post->ID ] = get_post( $post->ID );
} else {
$no_term_results[ $post->ID ] = get_post( $post->ID );
}
}
// Output grouped results.
if ( ! empty( $grouped_results ) ) {
foreach ( $grouped_results as $grouped_result ) {
echo '<h3>' . esc_html( $grouped_result['term']->name ) . '</h3>';
foreach ( $grouped_result['results'] as $result ) {
?>
<div class="searchwp-live-search-result" role="option" id="" aria-selected="false">
<p>
<a href="<?php echo esc_url( get_permalink( $result->ID ) ); ?>">
<?php echo wp_kses_post( get_the_title( $result->ID ) ); ?> &raquo;
</a>
</p>
</div>
<?php
}
}
}
// Output ungrouped results.
if ( ! empty( $no_term_results ) ) {
foreach ( $no_term_results as $result ) {
?>
<div class="searchwp-live-search-result" role="option" id="" aria-selected="false">
<p>
<a href="<?php echo esc_url( get_permalink( $result->ID ) ); ?>">
<?php echo wp_kses_post( get_the_title( $result->ID ) ); ?> &raquo;
</a>
</p>
</div>
<?php
}
}

今日からより良いWordPress検索エクスペリエンスを作成しましょう

役に立たない検索結果で訪問者を失うことはもうありません。SearchWPを使用すると、独自のスマートなWordPress検索をすばやく簡単に作成できます。

Get SearchWP Now
複数の検索エンジンアイコン