I am trying to retrieve the slug of the current WordPress page outside the loop. The title of the page returns with wp_title ()
, but how can I get the slug?
<li>
<a href="/slug-of-current-page/">
<?php wp_title('', true); ?>
</a>
</li>
I am trying to retrieve the slug of the current WordPress page outside the loop. The title of the page returns with wp_title ()
, but how can I get the slug?
<li>
<a href="/slug-of-current-page/">
<?php wp_title('', true); ?>
</a>
</li>
You must be logged in to post a comment.
Use the global variable
$post
:As per other answers, slug is stored in the
post_name
property. While it could be accessed directly, I prefer the (underused)get_post_field()
function for accessing post properties which have no proper API for them.It requires post provided explicitly and doesn’t default to the current one, so in full for the current post it would be:
EDIT 5 APRIL 2016
After digging for more reliability, I ended up doing this answer to the following post which leads to this edit: (Be sure to check it out)
The most reliable method till date I could come up with is the following:
This way, you are 99.9999% sure that you get the correct data every time.
ORIGINAL ANSWER
Another safer alternative to this problem is using
get_queried_object()
which holds the current queried object to get the page slug which is held by thepost_name
property. This can be used anywhere in your template.$post
can be used, but it can be unreliable as any custom query or custom code can change the value of$post
, so it should be avoided outside of the loop.Using
get_queried_object()
to get the current page object is much more reliable and is less likely to be modified, unless you are using the evilquery_posts
which breaks the main query object, but then that is all up to you.You can use the above as follow
The simple way to get the slug is with:
Given the code example, it looks like what you really need is a link. In that case, you can use get_permalink(), which can be used outside of the loop. That should do what you need more reliably than using the post slug.
You can simply explode the slug from the request.
This works for all posts, pages, custom routes.
Might be an old question, but I created the functions get_the_slug() and the_slug() based on your answers.
If you want a more under-the-hood answer, you can use the following SQL query to fetch all of the posts that are either posts, pages, or custom taxonomies at any time, even if no hooks have fired whatsoever as of yet.
Raw SQL:
This works even on the very first line of your functions file, even prior to the
mu_plugins_loaded
orinit
hooks.@note
This is assuming you have a standard database prefix
wp_posts
. If you need to account for variable prefixes, you can obtain the correct post table through PHP pretty easily by doing the following:Then run with either
$wpdb
,mysqli
, or aPDO
instance. Since there is no user input in this query, it is safe to run without a prepared statement as long as you do not inject any variables into it.I would suggest storing this as a private static value of a class, so it can be accessed without having to fire the query again more than once per page for best performance, something like this:
Usage
You get the gist. If you need further details, you can fetch them as per normal with
new WP_Post( get_the_ID() );
This will let your check the posts at any time, even if the wordpress loop has not hit a point where it finds your request agreeable. This is a slightly more optimized version of the same query run by the WordPress core itself. This one filters out all of the junk you would not want returned, and just gives you a nicely organized list with the relevant author id, post type, slug, and visibility. If you need further details, you can fetch them as per normal with
new WP_Post($id);
, or use any of the other native WordPress functions with any of the relevant table rows, even outside of the loop.I use a similar setup in a couple of my own custom themes and plugins, and it works pretty great. It’s also secure and doesn’t leave internal data floating around in the global scope where it can be overridden like most stuff in WordPress does.
This is the function to use when wanting to retrieve the slug outside of the loop.
Answer found here: How to Retrieve the Slug of Current Page in WordPress?
If you are in the loop then the other answers will help you.
If not (for example you are hooking on
init
orplugins_loaded
) you can resort to a PHP primitive likeparse_url()
.Here’s a function that works in both cases:
Please note that this approach only works on posts/pages at root level, because of how
basename()
works.Just further on @Matthew Boynes answer, if you’re interested in getting the parent slug (if any) also then I’ve found this function useful:
Eg to add the slug(s) to the body class:
Dynamic Page calling in WordPress.