Get page id by page name
Today the lesson is simple and I think many already know how, but those who do not know how to work with a database in WordPress and those who can benefit from this topic, I think, will appreciate it.
Get page id by page name. This is quite easy to do if you know how to query the database.. Adding a code snippet to a file functions.php your template WordPress.
1 2 3 4 5 6 7 8 |
function wds_get_ID_by_page_name($page_name) { global $wpdb; $page_name = strip_tags($page_name); $page_name = addslashes($page_name); $page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title ='".$page_name."'"); return $page_name_id; } |
You can display the page ID in any part of the template using the following code snippet.
1 2 3 4 |
<? $the_page_id = wds_get_ID_by_page_name('PAGE_NAME'); echo $the_page_id; ?> |
Don't forget to change PAGE_NAME to your page name, whose id you would like to return. Good luck in website development.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.