0

I have a defined set of seven different post types and would like to display an icon for each one in the search results list.

I've got the following code to display the post type but have no idea how to extend it into an if

<?php $post_type = get_post_type_object( get_post_type($post) );
echo $post_type->label ; ?>

Any help appreciated.

1
<?php 
   $post_type = get_post_type($post);
   switch ($post_type) {
       case "type1":
           echo "<img src='label1.png'/>";
           break;
       case "type2":
           echo "<img src='label2.png'/>";
           break;
   }
?>
  • You can also access the post type directly from the $post object: $post->post_type. – Geert Dec 5 '11 at 12:27
1

You could also do this with CSS. If you're using post_class() in the search results it will have the post-type available within each post's classes for styling each type differently.

Then you just apply a different background image as an icon for each post type class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.