Developer Note : WordPress Slugs

Last Updated:June 13, 2021

WordPress Slugs

How to get the slug of the current post?

Using global $post

$post variable gives you information about the page

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

You can use another method called get_post_field. You should inside the loop to call this function

$page_slug = get_post_field( 'post_name' );

If you are outside the loop you can use the same function with additional argument named $post_id

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