Customizing search results based on post type

[ad_1]

I am able to sort search results by post type with the following code below.

I need help customizing the results based on post types.

For example, if $type == ‘services’, I need to print just the title w/o the link. How can I do that?

if( have_posts() ) {
$types = array(‘services’, ‘solutions’, ‘page’);
foreach( $types as $type ){
if ( $type == ‘services’ ) {
echo ‘<h2>Services</h2>’;
} elseif ( $type == ‘solutions’ ) {
echo ‘<h2>Solutions</h2>’;
} else {
echo ‘<h2>Other</h2>’;
}
while( have_posts() ){
the_post();
if( $type == get_post_type() ){
printf( ‘<h2><a href=”%s”>%s</a></h2>’, esc_url( get_permalink() ), esc_html( get_the_title() ) );
the_excerpt();
}
}
rewind_posts();
}
}

[ad_2]
1 Comment
  1. Just use a case statement and step through each post type and output whatever you want accordingly.

    So if the post type is your “services”, then just output <h2>Services</h2> or whatever you want.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer