This function will echo / display your post or page slug on your WordPress Template. An important function to have if you are doing custom WordPress programming, Custom Themes, etc.
How To Create The Slug Function
You need to locate your functions.php file in your WordPress Theme located at “wp-content/themes/mythemename/functions.php”
Once you find the functions.php wile, cut and paste the following code anywhere with in the file and save and upload the new functions.php to your server.
function the_slug() {
$post_data = get_post($post->ID, ARRAY_A);
$slug = $post_data['post_name'];
return $slug; }
Call The Function To WordPress Template
Use the php code below to display the Post or Page Slug in your Template file.
<?php echo the_slug(); ?>
Example Use For The Slug
I use the slug function in my post images on Insite Website Design. Take a close look at the post image on top of this page. When I make a post, I create a an image and name it to match the post slug. You can do this with WordPress custom fields, but this is how I do it.
Podcast Video
Take a look at the Podcast video titled “WordPress Slug, Having Fun With The Slug” if you like to see a video demonstration of this tutorial.





March 6th, 2010 at 11:23 am
function remove_accent($str)
{
$a = array(’À’,'Á’,'Â’,'Ã’,'Ä’,'Å’,'Æ’,'Ç’,'È’,'É’,'Ê’,'Ë’,'Ì’,'Í’,'Î’,'Ï’,'Ð’,'Ñ’,'Ò’,'Ó’,'Ô’,'Õ’,'Ö’,'Ø’,'Ù’,'Ú’,'Û’,'Ü’,'Ý’,'ß’,'à’,'á’,'â’,'ã’,'ä’,'å’,'æ’,'ç’,'è’,'é’,'ê’,'ë’,'ì’,'í’,'î’,'ï’,'ñ’,'ò’,'ó’,'ô’,'õ’,'ö’,'ø’,'ù’,'ú’,'û’,'ü’,'ý’,'ÿ’,'Ā’,'ā’,'Ă’,'ă’,'Ą’,'ą’,'Ć’,'ć’,'Ĉ’,'ĉ’,'Ċ’,'ċ’,'Č’,'č’,'Ď’,'ď’,'Đ’,'đ’,'Ē’,'ē’,'Ĕ’,'ĕ’,'Ė’,'ė’,'Ę’,'ę’,'Ě’,'ě’,'Ĝ’,'ĝ’,'Ğ’,'ğ’,'Ġ’,'ġ’,'Ģ’,'ģ’,'Ĥ’,'ĥ’,'Ħ’,'ħ’,'Ĩ’,'ĩ’,'Ī’,'ī’,'Ĭ’,'ĭ’,'Į’,'į’,'İ’,'ı’,'IJ’,'ij’,'Ĵ’,'ĵ’,'Ķ’,'ķ’,'Ĺ’,'ĺ’,'Ļ’,'ļ’,'Ľ’,'ľ’,'Ŀ’,'ŀ’,'Ł’,'ł’,'Ń’,'ń’,'Ņ’,'ņ’,'Ň’,'ň’,'ʼn’,'Ō’,'ō’,'Ŏ’,'ŏ’,'Ő’,'ő’,'Œ’,'œ’,'Ŕ’,'ŕ’,'Ŗ’,'ŗ’,'Ř’,'ř’,'Ś’,'ś’,'Ŝ’,'ŝ’,'Ş’,'ş’,'Š’,'š’,'Ţ’,'ţ’,'Ť’,'ť’,'Ŧ’,'ŧ’,'Ũ’,'ũ’,'Ū’,'ū’,'Ŭ’,'ŭ’,'Ů’,'ů’,'Ű’,'ű’,'Ų’,'ų’,'Ŵ’,'ŵ’,'Ŷ’,'ŷ’,'Ÿ’,'Ź’,'ź’,'Ż’,'ż’,'Ž’,'ž’,'ſ’,'ƒ’,'Ơ’,'ơ’,'Ư’,'ư’,'Ǎ’,'ǎ’,'Ǐ’,'ǐ’,'Ǒ’,'ǒ’,'Ǔ’,'ǔ’,'Ǖ’,'ǖ’,'Ǘ’,'ǘ’,'Ǚ’,'ǚ’,'Ǜ’,'ǜ’,'Ǻ’,'ǻ’,'Ǽ’,'ǽ’,'Ǿ’,'ǿ’);
$b = array(’A',’A',’A',’A',’A',’A',’AE’,'C’,'E’,'E’,'E’,'E’,'I’,'I’,'I’,'I’,'D’,'N’,'O’,'O’,'O’,'O’,'O’,'O’,'U’,'U’,'U’,'U’,'Y’,’s’,'a’,'a’,'a’,'a’,'a’,'a’,'ae’,'c’,'e’,'e’,'e’,'e’,'i’,'i’,'i’,'i’,'n’,'o’,'o’,'o’,'o’,'o’,'o’,'u’,'u’,'u’,'u’,'y’,'y’,'A’,'a’,'A’,'a’,'A’,'a’,'C’,'c’,'C’,'c’,'C’,'c’,'C’,'c’,'D’,'d’,'D’,'d’,'E’,'e’,'E’,'e’,'E’,'e’,'E’,'e’,'E’,'e’,'G’,'g’,'G’,'g’,'G’,'g’,'G’,'g’,'H’,'h’,'H’,'h’,'I’,'i’,'I’,'i’,'I’,'i’,'I’,'i’,'I’,'i’,'IJ’,'ij’,'J’,'j’,'K’,'k’,'L’,'l’,'L’,'l’,'L’,'l’,'L’,'l’,'l’,'l’,'N’,'n’,'N’,'n’,'N’,'n’,'n’,'O’,'o’,'O’,'o’,'O’,'o’,'OE’,'oe’,'R’,'r’,'R’,'r’,'R’,'r’,'S’,’s’,'S’,’s’,'S’,’s’,'S’,’s’,'T’,'t’,'T’,'t’,'T’,'t’,'U’,'u’,'U’,'u’,'U’,'u’,'U’,'u’,'U’,'u’,'U’,'u’,'W’,'w’,'Y’,'y’,'Y’,'Z’,'z’,'Z’,'z’,'Z’,'z’,’s’,'f’,'O’,'o’,'U’,'u’,'A’,'a’,'I’,'i’,'O’,'o’,'U’,'u’,'U’,'u’,'U’,'u’,'U’,'u’,'U’,'u’,'A’,'a’,'AE’,'ae’,'O’,'o’);
return str_replace($a, $b, $str);
}
function post_slug($str)
{
return strtolower(preg_replace(array(’/[^a-zA-Z0-9 -]/’, ‘/[ -]+/’, ‘/^-|-$/’), array(”, ‘-’, ”), remove_accent($str)));
}
January 9th, 2010 at 8:42 pm
I wanted to add something that I found when using this function…
I needed to use the_slug() to dynamically display posts that were tagged the same as the slug name of the container page, and I needed to do it over the course of 4 loops on that single page. So I wanted to use the_slug() in conjunction with query_post – my query_post looked something like this:
‘News’,
‘orderby’=>’date’,
‘order’=>’DESC’,
‘tag’=>the_slug(),
‘posts_per_page’ => 2,
));
The problem was it would only work 1 time on page. When I attempted to recycle the loop, the_slug() would not work again.
SO, I declared at the top of my template php file:
get_header();
$slugger=the_slug();
Then, for each of my 4 loops, my query_post looked like this:
‘News’,
‘orderby’=>’date’,
‘order’=>’DESC’,
‘tag’=>$slugger,
‘posts_per_page’ => 2,
));
This time it worked like a charm! I was able to recycle that function for all 4 of my loops on page.
December 17th, 2009 at 2:11 pm
@ John, Thanks for the comments John. I’m glad the code helped you.
December 17th, 2009 at 10:14 am
Just what I was after. I want to use it to call a specific function file depending on the page landed on. That way I can maintain my functions easier and the main file doesn’t get too big. I have all general functions in the functions.php and then several other functions file named after the relevant pages. Such as registration_fncs.php
Your code has really helped me getting this working. Thanks for your efforts.
July 1st, 2009 at 11:44 am
Thanks… just what I was looking for. Surprising that WP doesn’t have this built in.
June 30th, 2009 at 3:22 am
I’m glad you found the tip helpful. Thanks.
June 23rd, 2009 at 9:09 pm
Thanks for the tip. I’ll be using it for element IDs.
May 20th, 2009 at 10:36 am
Great article! I’m loving your website;
May 14th, 2009 at 5:57 am
Great article! I’m loving your website;
April 17th, 2009 at 3:45 pm
[...] a look at the post titled “WordPress Post Slug and Page Slug Function” for a text version of this Podcast. | Subscribe via Feedburner iTunes [...]