最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Custom Post Type and single-posttype template

programmeradmin4浏览0评论

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
  • possible duplicate of Wordpress 3.3 custom post type with /%postname%/ permastruct? – Stephen Harris Commented Apr 17, 2012 at 14:32
  • not a duplicate, because I found a different solution – user15194 Commented Apr 17, 2012 at 14:35
  • The duplicity refers to the question not the answers. You seem to be wanting 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
  • Sorry, but I can not leave answer to my question. Requires at least 100 reputation points. I'll write tomorrow ;) – user15194 Commented Apr 17, 2012 at 15:00
Add a comment  | 

1 Answer 1

Reset to default 0

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' );
发布评论

评论列表(0)

  1. 暂无评论