How to retrieve the slug of current WordPress page ?

Page slug means the name of the page. When we add a new page in WordPress then WordPress automatically create the name of the page based on the page title we provide. For example the page slug of this page is how-to-retrieve-the-slug-of-current-wordpress-page .

At the admin end there is also a page slug for every single admin pages. When we create a custom admin menu, it has a page slug like this          wp-admin/admin.php?page=sample-page-slug . 

Now important thing is how we can retrieve the page slug if we want. There is a function in WordPress get_post_field()  for this work.


$post_slug = get_post_field( 'post_name', get_post() );
echo $post_slug;

Here ‘post_name’ is a post field. This function retrieve data from the post field based on the ID of the post. Some other post fields are – ‘post_type’, ‘post_status’, ‘post_content’, etc. When we click on a custom post type. We can see the post field and slug like this

wp-admin/edit.php?post_type=services

I hope this tutorial helps you to understand the concept clearly.

Thanks !

Leave a Reply

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