I like to get the ID of the selected front page.
My page uses an Template for that page.
I've read about get_option('page_on_front')
, but this didn't work for me.
Is there any function to get this ID?
This should do the trick.
global $wp_query;
$post = $wp_query->get_queried_object();
$post->ID;
This'll give you the ID for each page you're on.
get_option( 'page_on_front' )
should've worked though.
You could either go into your Dashboard > 'Pages' and then hover over the page that is using the front page template and hover over the title - on the bottom left of your screen you will see a string of information like this:
http://yourdomain.com/wp-admin/post.php?post=724&action=edit
Post=724, that part will be the ID of that page.
Or programmatically you could use this function and pass the slug of this page using the front page template:
function royal_get_id($page_slug) {
$page = get_page_by_path($page_slug);
if ($page) {
return $page->ID;
} else {
return null;
}
}
$value = royal_get_id('your-homepage-slug');
echo $value;
What you doing is correct then, just store it in a variable to use as you need to.
Also make sure you have set the page as the front page in your dashboard under 'Settings' > Reading or else this value will be empty as it is not set.
$front_id = get_option('page_on_front');
echo $front_id;
you can also get the global post and fetch its ID inside a loop - that way you get the ID of each page - as in WordPress all pages are posts.
global $post;
$post->ID
Settings -> Reading
admin page, is "A static page" selected on "Front page displays", and is a page selected in the "Front page" dropdown?