$attachment->post_title not displaying title

1. the_post_thumbnail

… is a wrapper for echo get_the_post_thumbnail. Hence the echo construct you are using is redundant. That’s not the core of your problem, but worth noting.

2. The empty attributes

In your HTML output, the attributes are empty, but they are created.

That leads to the likely conclusion, that the culprit is not the_post_thumbnail, but that the values for $attr['alt'] and $attr['title'] are empty.

If that’s the case, both trim(strip_tags( $attachment->post_excerpt )) and trim(strip_tags( $attachment->post_title )) must return empty strings.

You can verify (or falsify) that conclusion in two ways:

  • By passing static strings to the $attr array.
    Are the attributes being populated?
    That would mean the function works.
  • By dumping the questionable expressions output:
    var_dump( trim(strip_tags( $attachment->post_title )) );
    I would venture to guess that’ll output string(0) ""

If that’s the case, the next question is “why?”
And that can’t be answered conclusively without further information.
That might be disappointing, but is a good segue to:

3. WP_DEBUG

While developing, set that constant to true
It will both help you personally as well as make it possible for you to provide pertinent information when seeking help.