Solution Code
If you’re in a rush, here’s a one line solution:
$homepage_id = get_option('page_on_front');
get_option function
WordPress has many functions that can be used (or just a wordaround) to get the homepage ID.
One of those many functions is get_option.
get_option gives you an option value based on the option name you pass.
Few things to remember
- If the option doesn’t exist then it will return false.
- If the option doesn’t have any value, it will return false too.
- It will return unserialized value even if the option value is serialized.
Syntax
get_option($option, $return_value)
Where $option is a string and a required parameter. $return_value is not required so if you don’t pass it, it will take the default value as false. You can ask for array, boolean, float, integer, null, object, and string as a return value.
Examples
Get blog/site name
echo get_option( 'blogname' );
Get blog/site description
echo get_option( 'blogdescription' );
Get admin email
echo get_option( 'admin_email' );
Get date format from settings
echo get_option( 'date_format' );
Get time format from settings
echo get_option( 'time_format' );
Checkout WP Docs for more
get_option function on WordPress
functions get_option home_url is_home pages