php - Custom post type permalink returns bad url

I have problem with custom post type. In BO url's are ok example: page.plpost_type_namepost_title.When I try use

I have problem with custom post type. In BO url's are ok example: page.pl/post_type_name/post_title. When I try use get_the_permalink() or the_permalink() it return url like: /?post_type=cpt_wydarzenia&p=258

Code:

    $args = array(
            "label" => __( "Wydarzenia", "vilo" ),
            'labels' => $labels,
            'hierarchical' => false,
            'supports' => array('title', 'editor'),
            //'taxonomies' => array( 'category', 'post_tag' ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_position' => 5,
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => false,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => array(
                'slug' => 'wydarzenia',
                'with_front' => true
            ),
            'capability_type' => 'post',
            'menu_i

          con' => 'dashicons-book',
                 'supports' => array(
                 'title',
                  'thumbnail',
            'comments',
            'editor'),
    );

    register_post_type( 'cpt_wydarzenia', $args );
    flush_rewrite_rules();

I have problem with custom post type. In BO url's are ok example: page.pl/post_type_name/post_title. When I try use get_the_permalink() or the_permalink() it return url like: /?post_type=cpt_wydarzenia&p=258

Code:

    $args = array(
            "label" => __( "Wydarzenia", "vilo" ),
            'labels' => $labels,
            'hierarchical' => false,
            'supports' => array('title', 'editor'),
            //'taxonomies' => array( 'category', 'post_tag' ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_position' => 5,
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => false,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => array(
                'slug' => 'wydarzenia',
                'with_front' => true
            ),
            'capability_type' => 'post',
            'menu_i

          con' => 'dashicons-book',
                 'supports' => array(
                 'title',
                  'thumbnail',
            'comments',
            'editor'),
    );

    register_post_type( 'cpt_wydarzenia', $args );
    flush_rewrite_rules();
Share Improve this question edited Jul 15, 2020 at 18:12 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Jul 15, 2020 at 7:17 encenc 132 bronze badges 2
  • Is the post 258 published? If no, then it's normal to have the "ugly" URL. – Sally CJ Commented Jul 15, 2020 at 8:27
  • No it isn't. The post is planned. Is it possible, to planned post have normally url. I create incoming event based on custom post with planned public. – enc Commented Jul 15, 2020 at 8:43
Add a comment  | 

1 Answer 1

Reset to default 1

get_permalink() (which is used by get_the_permalink() and the_permalink()) returns the "ugly" URL if the post is not published, e.g. draft or scheduled, but you can use the following function instead which temporarily sets the post status to publish so that we'd get the pretty URL.

function get_future_permalink( $id ) {
    if ( $post = get_post( $id ) ) {
        $post->post_status = 'publish';
        return get_permalink( $post );
    }
    return '';
}

So instead of get_permalink( 258 ), you'd use get_future_permalink( 258 ), where 258 is the post ID.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742259802a4410692.html

相关推荐

  • php - Custom post type permalink returns bad url

    I have problem with custom post type. In BO url's are ok example: page.plpost_type_namepost_title.When I try use

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信