Change custom post-type rewrite

2019-05-28 23:01发布

I have a custom theme that uses custom post types for my portfolio e.g. mysite.com/portfolio/. Once I click on a specific portfolio item the URL changes to mysite.com/portfolio-type/name/

What's the easiest way to change the portfolio-type URL in the middle to another value e.g "showcase"?

Here the custom post type part of my functions.php:

    // CUSTOM POST TYPES

    function justins_custom_post_types() {


        // Portfolio

        $labels_portfolio = array(
            'add_new' => 'Add New', 'portfolio-type',
            'add_new_item' => 'Add New Portfolio Post',
            'edit_item' => 'Edit Portfolio Post',
            'menu_name' => 'Portfolio',
            'name' => 'Portfolio', 'post type general name',
            'new_item' => 'New Portfolio Post',
            'not_found' =>  'No portfolio posts found',
            'not_found_in_trash' => 'No portfolio posts found in Trash', 
            'parent_item_colon' => '',
            'singular_name' => 'Portfolio Post', 'post type singular name',
            'search_items' => 'Search Portfolio Posts',
            'view_item' => 'View Portfolio Post',
        );
        $args_portfolio = array(
            'capability_type' => 'post',
            'has_archive' => true, 
            'hierarchical' => true,
            'labels' => $labels_portfolio,
            'menu_position' => 4,
            'public' => true,
            'publicly_queryable' => true,
            'query_var' => true,
            'show_in_menu' => true, 
            'show_ui' => true, 
            'supports' => array( 'comments', 'editor', 'excerpt', 'thumbnail', 'title' ),
            'singular_label' => 'Portfolio',
        );
        register_post_type( 'portfolio-type', $args_portfolio );


    }

    add_action( 'init', 'justins_custom_post_types' );


    // CUSTOM TAXONOMIES

    function justins_custom_taxonomies() {


        // Portfolio Categories 

        $labels = array(
            'add_new_item' => 'Add New Category',
            'all_items' => 'All Categories' ,
            'edit_item' => 'Edit Category' , 
            'name' => 'Portfolio Categories', 'taxonomy general name' ,
            'new_item_name' => 'New Genre Category' ,
            'menu_name' => 'Categories' ,
            'parent_item' => 'Parent Category' ,
            'parent_item_colon' => 'Parent Category:',
            'singular_name' => 'Portfolio Category', 'taxonomy singular name' ,
            'search_items' =>  'Search Categories' ,
            'update_item' => 'Update Category' ,
        );
        register_taxonomy( 'portfolio-category', array( 'portfolio-type' ), array(
            'hierarchical' => true,
            'labels' => $labels,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'portfolio-type/category' ),
            'show_ui' => true,
        ));


        // Portfolio Tags   

        $labels = array(
            'add_new_item' => 'Add New Tag' ,
            'all_items' => 'All Tags' ,
            'edit_item' => 'Edit Tag' , 
            'menu_name' => 'Portfolio Tags' ,
            'name' => 'Portfolio Tags', 'taxonomy general name' ,
            'new_item_name' => 'New Genre Tag' ,
            'parent_item' => 'Parent Tag' ,
            'parent_item_colon' => 'Parent Tag:' ,
            'singular_name' =>  'Portfolio Tag', 'taxonomy singular name' ,
            'search_items' =>   'Search Tags' ,
            'update_item' => 'Update Tag' ,
        );
        register_taxonomy( 'portfolio-tags', array( 'portfolio-type' ), array(
            'hierarchical' => true,
            'labels' => $labels,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'portfolio-type/tag' ),
            'show_ui' => true,
        ));

}

Reference URL to the theme's portfolio section: http://html.orange-idea.com/commander/4-columns-portfolio/ (No problems with this one)

Single item: http://html.orange-idea.com/commander/portfolio-type/summer-time/ (/portfolio-type/ added in URL)

I would also like to mention that the theme consists of a single-portfolio-type.php file.

Thank you.

1条回答
淡お忘
2楼-- · 2019-05-28 23:12

Maybe this will do what you want:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  (.*)/portfolio-type/(.*)
RewriteRule .* http://html.orange-idea.com/%1/showcase/%2  [QSA,R=301,L]

It will redirect this:

http://html.orange-idea.com/commander/portfolio-type/summer-time/

To this:

http://html.orange-idea.com/commander/showcase/summer-time/

If it is the other way around, which I think it is:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  (.*)/showcase/(.*)
RewriteRule .* http://html.orange-idea.com/%1/portfolio-type/%2  [QSA,L]

It will map silently this incoming URL::

http://html.orange-idea.com/commander/showcase/summer-time/

To this:

http://html.orange-idea.com/commander/portfolio-type/summer-time/

查看更多
登录 后发表回答