xxxxxxxxxx
// Get the queried object and sanitize it
$current_page = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() );
// Get the page slug
$slug = $current_page->post_name;
xxxxxxxxxx
global $post;
if ($post->post_type == 'page') { // Checking if the current post is a page
$slug = get_post_field('post_name', $post->ID); // Retrieving the slug of the current page
echo $slug;
}