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
2 Answers
Reset to default 3You 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
版权声明:本文标题:menus - how to get post slug from url in wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741520151a2383132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论