WordPress query single post by slug

前端 未结 4 823
情书的邮戳
情书的邮戳 2020-12-25 09:24

For the moment when I want to show a single post without using a loop I use this:



        
4条回答
  •  独厮守ぢ
    2020-12-25 10:05

    a less expensive and reusable method

    function get_post_id_by_name( $post_name, $post_type = 'post' )
    {
        $post_ids = get_posts(array
        (
            'post_name'   => $post_name,
            'post_type'   => $post_type,
            'numberposts' => 1,
            'fields' => 'ids'
        ));
    
        return array_shift( $post_ids );
    }
    

提交回复
热议问题