How to get post slug from post in WordPress?

后端 未结 10 2193
我在风中等你
我在风中等你 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:13

    Best option to do this according to WP Codex is as follow.

    Use the global variable $post:

    <?php 
        global $post;
        $post_slug = $post->post_name;
    ?>
    
    0 讨论(0)
  • 2020-12-25 10:16

    You can retrieve it from the post object like so:

    global $post;
    $post->post_name;
    
    0 讨论(0)
  • 2020-12-25 10:18

    use global variable $post

    <?php 
        global $post;
        $post_slug=$post->post_name;
    ?>
    
    0 讨论(0)
  • 2020-12-25 10:23

    If you want to get slug of the post from the loop then use:

    global $post;
    echo $post->post_name;
    

    If you want to get slug of the post outside the loop then use:

    $post_id = 45; //specify post id here
    $post = get_post($post_id); 
    $slug = $post->post_name;
    
    0 讨论(0)
  • 2020-12-25 10:27

    this simple code worked for me:

    $postId = get_the_ID();
    $slug = basename(get_permalink($postId));
    echo $slug;
    
    0 讨论(0)
  • 2020-12-25 10:28

    You can get that using the following methods:

    <?php $post_slug = get_post_field( 'post_name', get_post() ); ?>
    

    Or You can use this easy code:

    <?php
        global $post;
        $post_slug = $post->post_name;
    ?>
    
    0 讨论(0)
提交回复
热议问题