I want to extract the slug from post permalink when pretty permalinks is enabled.
If the permalink is:
http://sitename.com/category/best-post
Grabbing only “best-post”.
(My end goal is to query posts by posts urls)
Any help will be appreciate!
I want to extract the slug from post permalink when pretty permalinks is enabled.
If the permalink is:
http://sitename.com/category/best-post
Grabbing only “best-post”.
(My end goal is to query posts by posts urls)
Any help will be appreciate!
Comments are closed.
Split the string by the slashes and take the last bit:
You can simply use the global $post object like below:
within the loop you can use,
global $post;
$post_slug=$post->post_name;
or
and outside the loop you can do it like this,
global $wp_query;
$post_id = $wp_query->post->ID
Not tested 100% but it should work for you