WordPress: Get the post slug


/ Published in: PHP
Save to your folder(s)

This function returns the slug fot the given post...


Copy this code and paste it in your HTML
  1. // @ WordPress
  2. // Returns the post slug
  3. // Make sure to use this inside the loop
  4. //----------------------------------------------------------
  5. function the_slug($postID="") {
  6.  
  7. global $post;
  8. $postID = ( $postID != "" ) ? $postID : $post->ID;
  9. $post_data = get_post($postID, ARRAY_A);
  10. $slug = $post_data['post_name'];
  11. return $slug;
  12. }
  13. //----------------------------------------------------------
  14. // to display current post/page slug: <?php echo the_slug(); ?>
  15. // to display specific post/page slug add the post id parameter: <?php echo the_slug('23'); ?>
  16. // to get var: <?php $slug = the_slug(); ?> or <?php $slug = the_slug('23'); ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.