Get Current Page Name WordPress

Written by
Published
Typical Read
3 minutes

Get the current page name in WordPress — the WordPress way. No hacks or referencing globals. A clean and simple approach using the get_post_field function to get current page name WordPress.

get current page name wordpress

tl;dr

Quit using a hacky workaround to get the page name/slug a user is currently on in WordPress. No need to reference global variables, get_queried_object, or roundabout filers. Instead, use the get_post_field function to get current page name WordPress:

// Returns the current page slug a visitor is on.
$slug = get_post_field( 'post_name', get_post() );

Get Current Page Name WordPress

A common question WordPress developers have is how to get the current page name or slug a user is on. This is useful information for a number of reasons:

  • Enqueue a specific JS or CSS based on page
  • Add a content or another filter based on the page slug
  • Process some custom functionality like a form submission based on the current page

There are several way’s to get current page name WordPress The most simple solution is to use the get_post_field function.

The get_post_field function.

get_post_field retrieves data from a post field based on the current or defined post ID allowing us to get current page name WordPress.

get_post_field( string $field, int|WP_Post $post = null, string $context = 'display' )
  • $field – Required. The post field name.
  • $post – Optional. The post ID or post object. Defaults to the global $post.
  • $context – Optional. How to filter the field. Accepts ‘raw‘, ‘edit‘, ‘db‘, or ‘display‘. The default value is ‘display‘.

Examples of $field include:

  • post_type
  • post_status
  • post_content
  • post_name (that’s what we’ll use to get the page slug)

For information, see check out the WordPress documentation.

How to get the current page slug in WordPress.

It’s a simple one-liner to return the current page slug a user is on:

// Returns the current page slug a visitor is on.
$slug = get_post_field( 'post_name', get_post() );

If you’re outside the loop, you’ll need to pass the post ID to the second argument, see below:

$slug = get_post_field( 'post_name', $post_id );

As mentioned before, there’s more than one way to get the slug of the current page in WordPress. Depending on your needs and placement, a different approach may work better.

Other methods to get the current page slug.

Another common technique to return the page slug in WordPress is to use the global $post object (see below). For more information, check out the WP_Post documentation.

global $post;
$slug = $post->post_name;

$post can be unreliable in any custom query or code since it can be changed. Avoid using it outside of the loop to prevent this.

You can also use the get_queried_object to return a page slug:

global $wp_query;
$post = $wp_query->get_queried_object();
$slug = $post->post_name;

But wait, there’s more. Another option is to use the global $wp object:

global $wp;
$slug = add_query_arg( array(), $wp->request );

Lastly, another, but more inaccurate option is to use the basename:

$slug = basename( get_permalink() );

In Conclusion

Doesn’t clean code just make you feel all warm and tingly inside? This solution to get current page name WordPress does just that. A simple, one-line function call to return a page slug.

Looking for some more useful WordPress tips, check out these other articles:

Join the conversation.

Your email address will not be published. Required fields are marked *

All comments posted on 'Get Current Page Name WordPress' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.