Function not working at one place

This is because you have functions that work only inside of the WordPress Loop like get_the_id(). This will return the correct value inside the WP Loop in template files, but when called in other places does not return the correct value and check fails.

See this for more information on the WordPress loop

https://codex.wordpress.org/The_Loop

When outside the loop you can either use the global $post object to get the id like this

global $post
$id = $post->ID;

Alternately you can try this

$id = get_queried_object_id();