Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question

7 Answers 7

up vote 28 down vote accepted

use global variable $post.

    global $post;
share|improve this answer
Thank you. Your solution works great. Just need to echo the slug: <?php global $post; $post_slug=$post->post_name; echo $post_slug; ?> – sarytash Feb 13 '12 at 12:13
Like sarytash said, you need to echo it. So, this'd be ideal: <?php global $post; echo $post->post_name; ?> – its_me Oct 11 '13 at 15:59

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.

share|improve this answer
This is the full URL though, not just the slug. – Fred Nov 21 '14 at 15:09

As per other answers slug is stored in post_name property. While it could be accessed directly I prefer the (underused) get_post_field() function for access 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 current post it would be:

$slug = get_post_field( 'post_name', get_post() );
share|improve this 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 the post_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 evil query_posts which breaks the main query object, but then that is all up to you.

You can use the above as follow

if ( is_page() )
    $slug = get_queried_object()->post_name;
share|improve this answer

The simple way to get the slug is with:

<?php echo basename(get_permalink()); ?>
share|improve this answer

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:

function mytheme_get_slugs() {
    if ( $link = get_permalink() ) {
        $link = str_replace( home_url( '/' ), '', $link );
        if ( ( $len = strlen( $link ) ) > 0 && $link[$len - 1] == '/' ) {
            $link = substr( $link, 0, -1 );
        return explode( '/', $link );
    return false;

Eg to add the slug(s) to the body class:

function mytheme_body_class( $classes ) {
    if ( $slugs = mytheme_get_slugs() ) {
        $classes = array_merge( $classes, $slugs );
    return $classes;
add_filter( 'body_class', 'mytheme_body_class' );
share|improve this answer

An alternative to Arvind's solution is to use the sanitize_title function:

In this specific case:

<li><a href="/<?php $slug = sanitize_title( get_the_title(), $fallback_title ); 
echo $slug; ?>/"><?php wp_title("",true); ?></a></li>
share|improve this answer
The slug is editable. A post titled Hello World! may get the slug random-nonsense. – toscho Feb 13 '12 at 16:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.