get_the_ID()
is a post tag. It is meant to be used inside a Loop. Per a comment above you are using it outside the Loop.
No, it’s not inside Loop. The code is simply posted within another
div, which holds slides for a slideshow.
get_the_ID
like most post tags depends on a global
variable called $post
. If that variable is not populated, or is set to the wrong post, you get odd results. I think that is why you think this works ‘randomly’.
A quirk of WordPress functioning is that the $post
object is set well before the theme templates load, but only for some pages, which means for some pages some functions will work even when used incorrectly outside a Loop.
I think you probably want to be using get_queried_object
to get the ID rather than depending on get_the_ID()
but there are caveats with that too and the context you are using this in is still not clear.
$pobj = get_queried_object();
$id = $pobj->ID;
Related:
https://wordpress.stackexchange.com/a/98138/21376
https://wordpress.stackexchange.com/a/99545/21376
https://wordpress.stackexchange.com/a/91254/21376