I needed to get just the post slug for a project I have been working on. I found several sites that had similar ways of getting the post slug but I came up with an approach that utilizes WordPress’ get_permalink() and PHP’s basename().
The common approach:
$post_obj = $wp_query->get_queried_object();
$post_ID = $post_obj->ID;
$post_title = $post_obj->post_title;
$post_slug = $post_obj->post_name;
My approach:
$slug = basename( get_permalink() );
This so very nearly fixed my problem, but I cannot get it to work properly outside of the loop. Is there a way of getting the actual url of the page and stripping it with just php?
Hey Dave, Have you tried passing the ID parameter? It would look something like this, get_permalink(id);
global $post;
echo $post->post_name;
๐
Absolutely brilliant. I searched for this for half an hour before I read your post and it works perfectly. Thank you!
Great approach! Super simple. I like it.
Thank you Josh, works great!
Well this is definately a nice and easy way to do it. Thank you very much.
Nice approach. Very clever. Thanks for posting it!
Thanks for this code, very handy. Just noticed though that the second method doesn’t return the slug if the page / post is the nominated home page, it just returns the root folder. Hope this helps, Charlotte
Slick solution. Thanks a ton!
Just what I was looking for.
I searched long and hard for slug, when it’s post_name and permalink.
How about the temporary permalink when it’s still a draft?
Permalink returns the now permalink version.
It’s so cool! ๐
Very nice, I was searching how to get slug for parent page ๐ Thanks
You saved me Josh ๐ thanks very brilliant
Thank you so much! I have been looking for this for over a day. Thank you!
very nice!
thanks for posting this
Yup this is definitely the best way to do this, thank you sir
Simple and clean … thank you!
Thanks for that! Really simple.
Thanks a lot
I use this within the loop.
<?php $post->post_name; ?>
Am I missing something here? If you are within the loop, isn’t the easiest thing to use @od3n’s suggestion above: $post->post_name
If you are using get_permalink(), don’t you have to be in the loop? If you are, then I would think that @od3n’s suggestion is the fastest and most simple.
Here’s another suggestion that I believe should work most of the time with the bonus that you can get the parent slug too:
$path_arr = explode(‘/’,$_SERVER[‘REQUEST_URI’]);
$page_slug = $path_arr[3];
$parent_page_slug = $path_arr[2];
This is an update of my previous comment. If your WordPress install is not in the root of the server, you’ll want to use this instead:
$section_path_arr = explode(‘/’,str_replace(get_bloginfo(‘wpurl’),”,$_SERVER[‘REQUEST_URI’]));
$page_slug = $path_arr[2];
$parent_page_slug = $path_arr[1];
Well that does work, but if you have any other permalink settings than the default the order in the array will be different. So I would be careful writing something like that in any other case than on your own site, that way you will have full control over this all the time.
Also – thanks for a neat tip, worked very well outside the loop as well for me.
I should add that if you are in the loop, you should also be able to use this code to get the parent:
$parent_page = get_post($post->post_parent);
$parent_page_slug = $parent_page->post_name;
Keep in mind that using this method though as opposed to the one I showed previously will probably run a little slower because you have to query the database. Database queries in PHP apps, are almost always slower than manipulating arrays and such. Not a big deal for a low trafficked site though.
Hey, it is great, does basename() function do heavy work like string operations? I mean, less code but better performance?
Great question. Unfortunately, I don’t know the answer.
This is EXACTLY what I was looking for. Thank you so much for posting this!!
Worked like a treat, thanks!
very nice indeed! one question though… any idea to do this backward? i.e. to get an id of a slug? i was hoping to use it in “exclude=” since it only accepts id. thanks
As a matter of fact I’ve needed to do this myself once before. ๐ If you are trying to get the ID of a slug, this works with pages and this might work with posts.
wow! thanks a lot!
Great approach Josh!
It’s nice to see some of the built-in PHP functionality being used as it should!
Thanks Kevin. Your site looks great by the way!
Josh, thanks a lot for this, it saved me hours of my time to figure this out. Much appreciated!
Wonderful tip!!! …. Its people like you that make WP such a joy to work with ๐
Many Thanks!
:-p
Very useful. Thanks!
Genious!.. simply brilliant. Thxs a lot
PERFECT!
You are a savior. This was driving me nuts, yours works perfect.
Came across this page during a brain fart, only to find a better way to do it. Thanks man.
FYI, I was wowed by the animated header, nice work. ๐
Thank you very much. My problem solved.
Mine too …. Wonderfully simple!! Thanks!
Very handy. I just made use of this as a way to query a custom taxonomy. Thanks Josh.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$slug = basename(get_permalink());
$args = array(
'post_type'=> 'project',
'work_type' => $slug,
'posts_per_page' => 12,
'order' => 'ASC',
'paged' => $paged );
query_posts( $args );
Cool, Chad. Glad you found it useful.
Thanks Josh! This is a neat solution! Using ‘get_post’ returns the entire post object, which sounds like an overkill just to get the slug. They should add a function to get the post slug from the id !
– Rutwick
You are a hero. Thanks so much!
Elegant, simple, and very quick. Thank you.
This doesn’t work with the default parameterized link structure (?page_id=21 .. etc)
I came across this:
sanitize_title(get_the_title());
which works just as well
this is great, worked a charm. thanks!!