Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
not a duplicate, because I found a different solution – wikicms Apr 17 '12 at 14:35
The duplicity refers to the question not the answers. You seem to be wanting 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. – Stephen Harris Apr 17 '12 at 14:59
Sorry, but I can not leave answer to my question. Requires at least 100 reputation points. I'll write tomorrow ;) – wikicms Apr 17 '12 at 15:00

1 Answer

up vote 0 down vote accepted

I 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' );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.