tags:

views:

217

answers:

2

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.

+2  A: 

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 ;?>
songdogtech
oh, how do i get the current id of a page if I'm not in a loop?
John
edited above....
songdogtech
A: 

dont use the get_the_id() method because this will print the id if you want to get the id then use the_id() method in loop only. This will give you the post id. if you want to use customize query in wordpress then use wp_query method which is provided by wordpress.

wordpressapi