Say you’ve added a few categories to your WordPress back-end. The default WordPress search displays only posts, but you would like to include your categories in the WordPress search results as well. Include the following snippet in your search.php template, and you should be good to go!
$terms = get_terms( 'category', array( 'name__like' => $s, 'hide_empty' => true // Optional ) ); if ( count($terms) > 0 ){ echo '<ul>'; foreach ( $terms as $term ) { echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a></li>'; } echo '</ul>'; }
No Comments