admin管理员组

文章数量:1122846

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?

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?

Share Improve this question asked Jun 11, 2014 at 12:23 RinkyyRinkyy 131 silver badge4 bronze badges 5
  • What does the 'page_on_front' option return for you? – hereswhatidid 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
Add a comment  | 

4 Answers 4

Reset to default 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.

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

本文标签: frontpageID of FrontPage