Support » Everything else WordPress » [solved] Slug as string

  • I just wanted to share the solution to a problem that occurred to me and obviously many others. While searching the forums for a way to post the slug in my templates all I found at first was the suggestion to use the post title and run a sanitize function on it. Unfortunately that’s not giving you the actual slug, only if you happened to name both identically. Digging a little in older posts I found something that was nearly what I was searching for.

    Here is a function that gives you the actual slug:

    function the_slug() {
    	$post_data = get_post($post->ID, ARRAY_A);
    	$slug = $post_data['post_name'];
    	return $slug;
    }

    Put the above code in your templates function.php, then you can use:

    <?php echo the_slug(); ?>

    in your template to get the slug.

    Hope this helps.

Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[solved] Slug as string’ is closed to new replies.