WordPress: Get post or page slug with or without ID

Last updated on

To get the post or page slug within your loop

global $post;

$slug = $post->post_name;

In this example we can get get the post or page slug with the ID of 7.

$slug = get_post_field( 'post_name', 7 );
1 Star2 Stars3 Stars4 Stars5 Stars 5.00 (2 votes)

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

Leave a Reply

avatar
  Subscribe  
Notify of