First, you have a very basic PHP mistake: single quotes mean “string literal”. Variables won’t be processed. You are literally putting “$variable” into the generated code.
Second, look at the third parameter of get_post_meta()
$single (boolean) (optional)
If set to true then the function will
return a single result, as a string. If false, or not set, then the
function returns an array of the custom fields. This may not be
intuitive in the context of serialized arrays. If you fetch a
serialized array with this method you want $single to be true to
actually get an unserialized array back. If you pass in false, or
leave it out, you will have an array of one, and the value at index 0
will be the serialized string.
Default: false
You are asking for a single variable as a string, and are then trying to iterate over it as if it were an array. I am surprised you aren’t getting errors. (see: https://wordpress.stackexchange.com/a/95983/21376)
It looks to me like you need to drop that third parameter or set it to false
(the default), which will return an array
that you can iterate over.