Get Current Page Slug WordPress

A post’s or page’s slug can be retrieved by accessing the post object’s post_name property. There are several ways to get the current page’s or post’s slug in WordPress. The first method involves utilizing the global $post variable while the second method uses the get_post_field function.

Option 1: Use Global Post Variable

    global $post;
    $post_slug=$post->post_name;

Option 2: Use the get_post_field Function

$slug = get_post_field( 'post_name', get_post() );

That’s all there is to it!

Leave a Reply

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