WordPress Get Page ID From Page Slug
WordPress Get Page ID From Page Slug
Super handy snippet of the day. Ever need to get the WordPress page ID based on the slug of the page? Yeah, me to! I use this function quite frequently, hopefully it can help you out. To start, add the following function to your active themes function.php file:
// Get ID of page by slug function get_id_by_slug($page_slug) { $page = get_page_by_path($page_slug); if ($page) { return $page->ID; } else { return null; } }
Next, use this function on the frontend of your theme. You could do something like
//The following will output the title of the page we're grabbing $page = get_id_by_slug( 'about' ); echo get_the_title( $page );
And there’s a little snippet for you that I bet will come in quite handy!
Share Your Thoughts