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!
example.com/%postname%
permalink structure for a custom post type. That's what the linked question addresses. Unfortunately it's not really something that can/should be done in WordPress - so I don't think there are any better answers than those given.