Skip to content

Instantly share code, notes, and snippets.

@jasondmoss
Last active August 26, 2017 12:45
Show Gist options
  • Save jasondmoss/7344278 to your computer and use it in GitHub Desktop.
Save jasondmoss/7344278 to your computer and use it in GitHub Desktop.
Get WordPress Post Slug
<?php
/**
* Get post slug
*
* @param integer $objectId (Optional) Post ID
* @param boolean $echo (Optional) Whether to echo or return value.
*
* @return string
* @access public
*/
function getSlug($objectId, $echo = false)
{
if (empty($objectId)) {
global $post;
$objectId = $post->ID;
}
$postData = get_post($objectId, ARRAY_A);
$slug = $postData['post_name'];
return ($echo) ? _e($slug) : $slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment