Side Area
Including categories on Wordpress search results page - Pim Ganzeboom
Freelance Web Developer in Antwerpen
Web Development, Webdesign, Web design, Antwerpen, Antwerp, Freelancer, Freelance, Front-End Developer, Frontend Developer, React
post-template-default,single,single-post,postid-566,single-format-standard,ajax_fade,page_not_loaded,,vertical_menu_enabled,select-child-theme-ver-1.0.0,select-theme-ver-3.2.1,wpb-js-composer js-comp-ver-4.2.3,vc_responsive

Including categories on WordPress search results page

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

Post a Comment