The problem appears when the cache is on because the footer is cached the same for all pages.
You either move it outside the footer block, but this might not be easy if you need it in a specific case.
Or you can create different cache for the homepage.
For this you can create an after
plugin on the Magento\Theme\Block\Html\Footer::getCacheKeyInfo
method.
Here is how you can create a plugin: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html
and your plugin class can look like this:
namespace [Vendor]\[Module]\Plugin\Html;
class FooterPlugin
{
/**
* @var \Magento\Framework\App\RequestInterface
*/
private $request;
public function __construct(\Magento\Framework\App\RequestInterface $request)
{
$this->request = $request;
}
public function afterGetCacheKeyInfo(
\Magento\Theme\Block\Html\Footer $subject,
array $result
) {
$handle = $this->request->getFullActionName();
$homeHandle = 'cms_index_index';
$result[] = ($handle === $homeHandle);
return $result;
}
}
CLear the cache and you should now have 2 cache versions for the footer. One for homepage and one for the rest of the pages.
Bonus: Don't use Object manager in templates. Use the approach Amit Bera showed to get your content into the page.
(i didn't test the code so watch out for typos).