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!
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!
Share Improve this question edited Apr 17, 2012 at 14:32 Stephen Harris 32.6k6 gold badges84 silver badges118 bronze badges asked Apr 17, 2012 at 13:58 user15194user15194 4 |1 Answer
Reset to default 0I found solution. Big Thanks @Scribu Alternative to query_posts for main loop? Now works fine and no more 404 error.
function wpse49295_alter_the_query( $request )
{
$dummy_query = new WP_Query();
$dummy_query->parse_query( $request );
if ( $dummy_query->is_single() )
$request['post_type'] = 'topic';
return $request;
}
add_filter( 'request', 'wpse49295_alter_the_query' );
example/%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. – Stephen Harris Commented Apr 17, 2012 at 14:59