WordPress get_the_ID() returns nothing but get_the_title() returns title?

I’m struggling with wordpress templates. In one of my theme templates I have the following lines

<?php
echo get_the_ID(); // displays nothing
echo get_the_title(); // displays "Hello World! this is title"
?>

Why does the get_the_ID() return nothing ? I want the page id of the current page I’m on.

Related posts

Leave a Reply

2 comments

  1. This works within the loop:

    <?php $this_page_id = get_the_ID(); echo $this_page_id ; ?>
    

    Outside of the loop:

    <?php $this_page_id = $wp_query->post->ID; echo $this_page_id ;?>