For this to work, you will need to have your search results ordered by the post type.
function order_by_pt($where,$qry) {
if (is_main_query() && $qry->is_search()) {
global $wpdb;
$where = $wpdb->posts.'.post_type DESC';
}
return $where;
}
add_action('posts_orderby','order_by_pt',1,2);
Then a Loop like this one will do what you want, if I understand you.
$type_title="";
while (have_posts()) {
the_post();
// this is where the title is checked and printed
if (get_post_type() !== $type_title) {
$type_title = get_post_type();
echo '<h2>Custom Post Type "'.$type_title.'" Results</h2>';
}
// switches are neater than a bunch of if/ifelses :)
switch (get_post_type()) {
case 'type_1' :
//Style type_1 ?>
<div id="type_1" <?php post_class(); ?>>
<div id="file-loader">
<a href="https://wordpress.stackexchange.com/questions/103331/<?php echo get_content_link( get_the_content() ); ?>">
<?php the_post_thumbnail(180, 230); ?>
</a>
</div>
<div id="file-hover"></div>
<h4>
<a href="https://wordpress.stackexchange.com/questions/103331/<?php echo get_content_link( get_the_content() ); ?>"><?php the_title(); ?></a>
</h4>
</div><?php
break;
case 'type_2' :
//Do different styling
echo '<p>',the_title(),'</p>';
break;
case 'some-other-type' :
break;
}
}
I formatted your code, put it is a Loop, and used a switch
instead of an if/else
chain.
That code should produce output like: