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

feedback

3 Answers

up vote 3 down vote accepted

use global variable $post.

<?php 
    global $post;
    $post_slug=$post->post_name;
?>
link|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 at 12:13
feedback

You can use get_permalink() outside of the loop, that should do what you need.

link|improve this answer
feedback

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>
link|improve this answer
The slug is editable. A post titled Hello World! may get the slug random-nonsense. – toscho Feb 13 at 16:36
feedback

Your Answer

 
or
required, but never shown

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