Get Post/Page Slug Using WordPress Function
This WordPress tip is from freelance web developer Maidul of Techsloution4u. When you’re creating a WordPress theme you might want to create some custom classes like CSS. This is where getting the slug of the current post can be very useful, which is what Maidul’s snippet does for you.
Simply copy and paste the following code below to your functions.php file:
function the_slug() { $post_data = get_post($post->ID, ARRAY_A); $slug = $post_data['post_name']; return $slug; }
After you’ve done that, just call the function within the loop to get the post or page slug.
// Put where you want to show in page or single etc. Use for custom class for each page <?php echo the_slug(); ?>
And that’s all it takes to do it! I hope a lot of you benefit from this little snippet.
Incoming search terms for the article:
- wordpress slug for custom post type
- get featured post using page slug
- wordpress get post by slug
- wordpress get page slug from database
- wordpress get page slug class css
- wordpress get page slug
- wordpress function slug
- wordpress function page slug
- to get featured image by page slug
- identify page slug wordpress