1

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?

5
  • What does the 'page_on_front' option return for you? Commented Jun 11, 2014 at 12:46
  • Why/in what way doesn't it work?
    – kraftner
    Commented Jun 11, 2014 at 12:46
  • It returns nothing
    – Rinkyy
    Commented Jun 11, 2014 at 12:50
  • On the Settings -> Reading admin page, is "A static page" selected on "Front page displays", and is a page selected in the "Front page" dropdown?
    – engelen
    Commented Jun 11, 2014 at 12:52
  • Yes and I want the ID of exactly this page :)
    – Rinkyy
    Commented Jun 11, 2014 at 12:53

4 Answers 4

2

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.

1
  • $post = $wp_query->get_queried_object(); $post->ID; Worked fine for me. Thanks!
    – Rinkyy
    Commented Jun 11, 2014 at 13:00
0

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;
1
  • Yes of course I can get the ID in the URL. But it should be dynamic depending on the settings in the backend. Your function returns a wrong ID for me?
    – Rinkyy
    Commented Jun 11, 2014 at 12:44
0

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;
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.