How to get the slug of a post in WordPress

If you want to get slug of the post from the loop then use:

global $post;
echo $post->post_name;

If you want to get slug of the post outside the loop then use:

$post_id = 45; //specify post id here
$post = get_post($post_id);
$slug = $post->post_name;

Leave a Reply

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

© 2019 Score Web Solutions
Top