Blog > Website Design > WordPress Post Slug and Page Slug Function

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.

Posted by : Dinusha | Apr 17, 2009

10 Responses to “WordPress Post Slug and Page Slug Function”

  1. Weblap.ro Says:
    10

    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)));
    }

  2. Erin Sean Says:
    9

    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.

  3. Dinusha Says:
    8

    @ John, Thanks for the comments John. I’m glad the code helped you.

  4. John Says:
    7

    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. ;-)

  5. joseph Says:
    6

    Thanks… just what I was looking for. Surprising that WP doesn’t have this built in.

  6. Dinusha Says:
    5

    I’m glad you found the tip helpful. Thanks.

  7. Leo Plaw Says:
    4

    Thanks for the tip. I’ll be using it for element IDs.

  8. BuyBedding Says:
    3

    Great article! I’m loving your website;

  9. churchill car insurance Says:
    2

    Great article! I’m loving your website;

  10. [...] a look at the post titled “WordPress Post Slug and Page Slug Function” for a text version of this Podcast. | Subscribe via Feedburner iTunes [...]

Leave a Reply

Name (required)

Mail (will not be published) (required)

Website

Blog Categories

Posts + Podcasts

Podcast Feeds

Design Portfolio

Design Services