WordPress – how to get the slug of your post or page?

Finding out the slug for a page or post is one of the most popular search terms on this blog. I find it a little bizarre that my little function generates so much interest, but that’s just one of the unexpected joys of blogging.

I have decided to follow-up my first post on getting the post slug, taking in to consideration the comments and main use of the function. Hopefully, though a small change, it is an improvement.

I’ve split it into two functions. The first forming the base for the second.

Function: get_the_slug()

function get_the_slug( $id=null ){
  if( empty($id) ):
    global $post;
    if( empty($post) )
      return ''; // No global $post var available.
    $id = $post->ID;
  endif;

  $slug = basename( get_permalink($id) );
  return $slug;
}

WordPress appears to be adopting the style of having get_ and the_ function prefixes to get or display content. So here is the complimentary function. I’ve removed some hooks that I don’t think anyone used, instead just applying one (well named) filter.

Function: the_slug()

/**
 * Display the page or post slug
 *
 * Uses get_the_slug() and applies 'the_slug' filter.
 */
function the_slug( $id=null ){
  echo apply_filters( 'the_slug', get_the_slug($id) );
}

Example use of the_slug() and get_the_slug() functions:

<div id="some-post-container-<?php echo get_the_slug(); ?>">
  The post slug is <strong><?php the_slug(); ?></strong>.
  Lorem whatsit here
</div>

Show future (scheduled) posts

Scheduling posts is a very useful technique, especially for blog authors. Why not add further value to your site by teasing visitors with upcoming posts? Place this code in your page template, and it will show the title of the next five future scheduled posts.

<?php query_posts( 'showposts=5&post_status=future' ); ?>
<?php if ( have_posts() ) : ?>
  <div class="future-posts">Future Posts</div>
  <?php while ( have_posts() ) : the_post(); ?>
    <div>
      <p class="future-title">
        <?php the_title(); ?> <br />
        <span class="datetime"><?php the_time( 'l, jS F Y' ); ?></span>
      </p>
    </div>
  <?php endwhile; ?>
<?php endif; ?>

I don’t use it personally, because I change my mind a lot and change the order of my scheduled posts quite often.

WordPress – the_slug() – Get post slug function

There is no template function (like the_title()) to get the slug of the current post.

There are two ways to do it:

1. Using basename() and get_permalink() to retrieve post slug.


  echo( basename(get_permalink()) ); 

2. Using global $post object to retrieve post slug.

<?php global $post; echo $post->post_name; ?>

I chose the former and added some hooks

3. Fully hookable function to retrieve the post slug

function the_slug($echo=true){
  $slug = basename(get_permalink());
  do_action('before_slug', $slug);
  $slug = apply_filters('slug_filter', $slug);
  if( $echo ) echo $slug;
  do_action('after_slug', $slug);
  return $slug;
}

This provides you with a simple way retrieve and display the post slug. I find this useful for applying unique CSS ids and classes to my HTML mark up.

<div id="some-post-container-<?php the_slug();?>">
 Lorem whatsit here
</div>

I’d love to hear what you use it for!

UPDATE

I’ve posted an update to this function – how to get the slug of your post or page?