Here’s the function. Copy it and paste it into your functions.php file:
function the_slug() {
$post_data = get_post($post->ID, ARRAY_A);
$slug = $post_data['post_name'];
return $slug;
}
Once done, simply call the function within the loop to get the post or page slug.
<?php echo the_slug(); ?>
Thanks to Maidul for the function!