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 );

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

Leave a reply

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