Custom Post Type and single-posttype template

Question

I created CPT topic

function wpse100_create_cpt() {
    register_post_type( 'topic', array(
        'labels' => array(
            //.....
            ),
        'public' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array(
            'slug' => '/topic', 
            'with_front' => false
            ),
        'has_archive' => false,
        'query_var' => true,
        ) );
add_action( 'init', 'wpse100_create_cpt' );

I choose permalink /%postname%, but by default URL mysite.tld/topic/postname, then I remove slug from URL mysite.tld/postname uses this code

function wpse100_remove_slug($post_link, $post) {
    if ( 'topic' != $post->post_type )
        return $post_link;

    return str_replace(get_bloginfo('url') . '/topic' , get_bloginfo('url'), $post_link);
}
add_filter( 'post_type_link', 'wpse100_remove_slugs', 10, 2 );

and then create template for CPT single-topic.php but I get 404 error. How I can to correct the error?

PS If I turn off function wpse100_remove_slug() it works fine, but I need use URL such as mysite.tld/postname

Thanks!

0
, user15194 1 year 2020-07-10T19:10:25-05:00 0 Answers 63 views 0

Leave an answer

Browse
Browse