Categories
Wordpress Tips and Tricks

How to Get Current Page Name in WordPress

This blog post will help you get current page name in WordPress. So if you are planning to create a WordPress plugin or want to fetch the current page then this simple code will be of great help.

Things You Need to Know

  • Set  $pagename var if you use permalinks. It is because if you don’t use them, WP doesn’t need the page slug, so it doesn’t set it up.
  • Don’t use $pagename if you use the page as a static front page.
  • Define $pagename  in the file wp-includes/theme.php, inside the function get_page_template()
  • The get_page_template() the function is called before page theme files are parsed, so it is available at any point inside the templates for pages.

Code to Get Current Page Name in WordPress

Use the following code to get current page name in WordPress websites:

$pagename = get_query_var('pagename');
if ( !$pagename && $id > 0 ) {
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
$post = $absa_query->get_queried_object();
$pagename = $post->post_name;
}

Trouble Shooting? Get Help From Us

This code works fine if done properly. But still, if you are unable to get current page name in WordPress then Request a Quote. Our WordPress support staff will be in touch with you within minutes and assist you.

Apart from offering WordPress services and support, we also accept HTML to WordPress template conversion requests.