Conditional tag for attachment posts

Most of the time you want to customize the output of different post (post, page, attachment or custom post type), you will want to use specific template files as it is easier to maintain. For example:

  • single.php for standard post and defualt template for other post types with no specific tempalte file
  • attachment.php for media attachements
  • image.php for image attachments
  • page.php for pages
  • custom-type.php for custom-type post

More info in WordPress template hierarchy.

Of course, you can also use conditional tags as suggested in the other answer or suggested by you in your question.

if( is_single() ) {
     //This is a post of any type except attachment or page
}

if( is_attachment() ) {
     //This is an attachment
}

if( is_page() ) {
      //This is a page
}

if( is_singular( 'post' ) ) {
      //This is a post (core standard post type)
}

if( is_singular( 'custom-type' ) ) {
      //This is a custom-type post
}