Most of the time we want to retrieve the slug of the current WordPress page outside the loop for applying it on page CSS if the query, or whatever for you want to use that page slug.
Here we have posted the guide that will help you to get the current page slug name wherever inside of page you want. Just follow the below step by step guides and within a minutes you can easily retrieve page slug.
How to Retrieve the Slug of Current Page in WordPress?
Basically, when we try to get the page slug out of loop it won’t display, Only the thing will display outside of loop is the page title with the help of its function called wp_title()
Read Also: Custom Registration or Sign Up Form in WordPress Without Using A Plugin
So how would we get the page slug? For retrieving the slug of page we have to use global
scope. And that’s what we are going to use. Let’s not waste time and get to the point.
Code Snippet of getting page slug.
Another Function of Retrieving the Page slug
Below is the one more function that you can use to retrieve the current page slug outside of loop. This function is based on get_post_field();
and applied the parameter post-name
.
Just copy and paste these codes and echo wherever you want.