How to Get the Post Slug


The post slug is the part of the URL of a post or page at the last section. E.g.:


There are two ways to get the slug in WordPress inside the loop. The first way is to access the $post object that is available inside the loop.

$slug =  $post->post_name;

The second way is to pass the whole permalink to the basename function, which will automatically process the URL and give us only the slug.

$slug = basename(get_permalink());

Happy coding!

Baki Goxhaj
I’m a web consultant specializing in WordPress. I love elegant solutions, quality code and a good laughter.
You may also like
How to Limit Content in WordPress
infinit next previous post looping
Infinite next and previous post looping in WordPress

Leave Your Comment

Your Comment*

Your Name*
Your Webpage