How can i get slug from url in wordpress. For example my url is https://ww.test.com/test-page/test-post/data-to-get
how can i get data-to-get
from url in wordpress. I tried following code
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
what is wrong here
2
Answers
You can achieve your output using below methods.
by using Global post variable.
using PHP
Suppose URL: https://ww.test.com/test-page/test-post/data-to-get
OUTPUT: data-to-get
I have checked and it’s work for me.
Let me know if this works for you.
There is a built-in function for that in wordpress:
url_to_postid
.