admin管理员组

文章数量:1291044

i want to send post slug through URL and get on other page. e.g, www.example/cars/mini-car how can i get 'mini-car' car type

i've found the way to get the id from url. that is;

$url = get_post_permalink(); //Get the url of the current post
 $substring_start_pos = strpos($url, 'pid=') + 4; //Find the position in $url string where the url number starts
 $url_number = substr($url, $substring_start_pos); //Extract the substring form the start position to the end of the url string
 $key_1_values = get_post_meta($url_number, '_song_name', true );
 echo "Your song is called $key_1_values"; 

i want to know how can i do with custom url (to get post slug of custom post type sent from previous page) like i mentioned as example

i want to send post slug through URL and get on other page. e.g, www.example/cars/mini-car how can i get 'mini-car' car type

i've found the way to get the id from url. that is;

$url = get_post_permalink(); //Get the url of the current post
 $substring_start_pos = strpos($url, 'pid=') + 4; //Find the position in $url string where the url number starts
 $url_number = substr($url, $substring_start_pos); //Extract the substring form the start position to the end of the url string
 $key_1_values = get_post_meta($url_number, '_song_name', true );
 echo "Your song is called $key_1_values"; 

i want to know how can i do with custom url (to get post slug of custom post type sent from previous page) like i mentioned as example

Share Improve this question asked May 9, 2018 at 18:55 Asad UllahAsad Ullah 131 silver badge3 bronze badges 2
  • $post->post_name doesn't work? are you outside the loop or inside the loop? If outside you can use get_post_field( 'post_name', get_post() ); – Orlando P. Commented May 9, 2018 at 19:16
  • 1 i am outside the loop. actually i want to display the list of that certain post type i am getting from url. – Asad Ullah Commented May 9, 2018 at 19:55
Add a comment  | 

2 Answers 2

Reset to default 3

You can use get_queried_object() which holds the current queried object from where you can get the page slug which is held by the post_name property.

You can use as:

if ( is_page() )
    // // Get the page slug using queried object
    $my_slug = get_queried_object()->post_name;

Hope this will help you.

it is stored in the post_name field in database. you can utilize the following code if you are inside a loop:

$the_slug = get_post_field( 'post_name' );

or if you are outside the loop:

$the_slug = get_post_field( 'post_name', $post_id);

where $post_id is the id of the post you want to retrieve from.

本文标签: menushow to get post slug from url in wordpress