More than 800+ satisfied clients and counting, since 2010

Blog

Get Current Page Name in WordPress Using Simple Code

Get Current Page Name in WordPressThis blog post will help you get current page name in WordPress. So if you are planning to create a WordPress plugin or component and are unable to fetch the current page then this simple code will be of great help.

Things You Need to Know

  • The $pagename var is only set 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.
  • $pagename is not set if you use the page as a static front page.
  • $pagename is defined in the file wp-includes/theme.php, inside the function get_page_template()
  • The get_page_template() 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 = $wp_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 PSD to WordPress template conversion and HTML toWordPress template conversion requests.

Written by

The author didnt add any Information to his profile yet