How to get post slug from post in WordPress?

Wordpress

Wordpress Problem Overview


I want to get "abc_15_11_02_3" from http://example.com/project_name/abc_15_11_02_3/. How can i do this?

Wordpress Solutions


Solution 1 - Wordpress

You can get that using the following methods:

<?php $post_slug = get_post_field( 'post_name', get_post() ); ?>

Or You can use this easy code:

<?php
    global $post;
    $post_slug = $post->post_name;
?>

Solution 2 - Wordpress

If you want to get slug of the post from the loop then use:

global $post;
echo $post->post_name;

If you want to get slug of the post outside the loop then use:

$post_id = 45; //specify post id here
$post = get_post($post_id); 
$slug = $post->post_name;

Solution 3 - Wordpress

You can do this is in many ways like:

1- You can use Wordpress global variable $post :

<?php 
global $post;
$post_slug=$post->post_name;
?>

2- Or you can get use:

$slug = get_post_field( 'post_name', get_post() );

3- Or get full url and then use the PHP function parse_url:

$url      = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url_path = parse_url( $url, PHP_URL_PATH );
$slug = pathinfo( $url_path, PATHINFO_BASENAME );

I hope above methods will help you.

Solution 4 - Wordpress

this simple code worked for me:

$postId = get_the_ID();
$slug = basename(get_permalink($postId));
echo $slug;

Solution 5 - Wordpress

Best option to do this according to WP Codex is as follow.

Use the global variable $post:

<?php 
    global $post;
    $post_slug = $post->post_name;
?>

Solution 6 - Wordpress

You can retrieve it from the post object like so:

global $post;
$post->post_name;

Solution 7 - Wordpress

Wordpress: Get post/page slug

<?php 
// Custom function to return the post slug
function the_slug($echo=true){
  $slug = basename(get_permalink());
  do_action('before_slug', $slug);
  $slug = apply_filters('slug_filter', $slug);
  if( $echo ) echo $slug;
  do_action('after_slug', $slug);
  return $slug;
}
?>
<?php if (function_exists('the_slug')) { the_slug(); } ?>

Solution 8 - Wordpress

use global variable $post

<?php 
    global $post;
    $post_slug=$post->post_name;
?>

Solution 9 - Wordpress

Here is most advanced and updated version what cover many cases:

if(!function_exists('the_slug')):
	function the_slug($post_id=false, $echo=true) {
		global $product, $page;

		if(is_numeric($post_id) && $post_id == intval($post_id)) {} else {
			if(!is_object($post_id)){}else if(property_exists($post_id, 'ID')){
				$post_id = $post_id->ID;
			}
			if(empty($post_id) && property_exists($product, 'ID')) $post_id = $product->ID;
			if(empty($post_id)) $post_id =  get_the_ID();

			if(empty($post_id) && property_exists($page, 'ID')) $post_id =  $page->ID;
		}

		if(!empty($post_id))
			$slug = basename(get_permalink($post_id));
		else
			$slug = basename(get_permalink());
		do_action('before_slug', $slug);
		$slug = apply_filters('slug_filter', $slug);
		if( $echo ) echo $slug;
		do_action('after_slug', $slug);
		return $slug;
	  }
endif;

This is collections from the best answers and few my updates.

Solution 10 - Wordpress

I came across this method and I use it to make div IDs the slug name inside the loop:

<?php $slug = basename( get_permalink() ); echo $slug;?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1990View Question on Stackoverflow
Solution 1 - WordpressKeyur PatelView Answer on Stackoverflow
Solution 2 - WordpressDomainView Answer on Stackoverflow
Solution 3 - WordpressYatin KhullarView Answer on Stackoverflow
Solution 4 - Wordpresshossein naghnehView Answer on Stackoverflow
Solution 5 - Wordpressfaryal ridaView Answer on Stackoverflow
Solution 6 - WordpressIgor YavychView Answer on Stackoverflow
Solution 7 - WordpresstheRanaView Answer on Stackoverflow
Solution 8 - WordpressICG DEVSView Answer on Stackoverflow
Solution 9 - WordpressIvijan Stefan StipićView Answer on Stackoverflow
Solution 10 - WordpressIisraelView Answer on Stackoverflow