How to get post slug from post in WordPress?

后端 未结 10 2274
我在风中等你
我在风中等你 2020-12-25 09:32

I want to get \"abc_15_11_02_3\" from http://example.com/project_name/abc_15_11_02_3/. How can i do this?

10条回答
  •  余生分开走
    2020-12-25 10:33

    You can do this is in many ways like:

    1- You can use Wordpress global variable $post :

    post_name;
    ?>
    

    2- Or you can get use:

    $slug = get_post_field( 'post_name', get_post() );
    

    3- Or get full url and then use the PHP function parse_url:

    $url      = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $url_path = parse_url( $url, PHP_URL_PATH );
    $slug = pathinfo( $url_path, PATHINFO_BASENAME );
    

    I hope above methods will help you.

提交回复
热议问题