Last active
March 2, 2020 19:00
-
-
Save Njengah/bb52b6571fb7e0bbb105bfb6d4072422 to your computer and use it in GitHub Desktop.
WordPress function to get post id by slug and test it with an action hook to wp_head for tutorial - https://njengah.com/get-post-id-by-slug-in-wordpress/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Function to get post ID By slug | |
*/ | |
function get_id_by_slug($page_slug, $slug_page_type = 'page') { | |
$find_page = get_page_by_path($page_slug, OBJECT, $slug_page_type); | |
if ($find_page) { | |
return $find_page->ID; | |
} else { | |
return null; | |
} | |
} | |
/** | |
* Testing by Display to Header | |
*/ | |
add_action('wp_head' , 'test_function'); | |
function test_function(){ | |
echo get_id_by_slug('login-page','page'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment