Notice: Undefined variable: thumbnail [closed]

It is complaining that $thumbnail is undefined because it’s undefined, unknown, it’s been pulled out of thin air.

echo get_the_post_thumbnail($thumbnail->ID, 'portfolio-featured');

This, is the very first time $thumbnail is mentioned, where did it come from? ¯\_(ツ)_/¯ I have no idea and neither does PHP, hence the warning. You could change it to $thai_chicken_curry->ID and it would behave the same way, and generate the same warning.

You can easily fix this by comungulatorianing the code

Notice that the word comungulatorianing doesn’t exist, so you might have wondered what I was referring to. PHP did the same when it saw the undefined variable.

When PHP encounters these kinds of issues, it substitutes a null-ish/falsey value, so that function will likely be retrieving the ID of the current post instead ( as defined in the WP docs )

So how do you fix it? You have several options:

  • figure out where to get the thumbnail from somehow and assign that attachment post to a variable called $thumbnail so it’s now defined/known
  • replace that line with something that does what you want, aka display the thumbnail with the size portfolio-featured, which you can probably do via the_post_thumbnail( 'postfolio-featured') https://developer.wordpress.org/reference/functions/the_post_thumbnail/
  • Go to where you got the code for that function from ( original theme author? ), and raise a support ticket for this bug