admin管理员组

文章数量:1122846

I want to change my custom post URL. I want to remove "item" from URL.

With the below code and with custom post type url plugin / my URL is:

Original URL : http://mywebsite/wp/item/america/mayer/

Want to change it to http://mywebsite/wp/america/mayer/

Without using htaccess.

WordPress Code

    function aitDirItemPostType() {

        register_post_type( 'ait-dir-item',
            array(
                'labels' => array(
                    'name'          => 'Items',
                    'singular_name' => 'Item',
                    'add_new'       => 'Add new',
                    'add_new_item'  => 'Add new item',
                    'edit_item'     => 'Edit item',
                    'new_item'      => 'New item',
                    'not_found'     => 'No items found',
                    'not_found_in_trash' => 'No items found in Trash',
                    'menu_name'     => 'Items',
                ),
                'description' => 'Manipulating with items',
                'public' => true,
                'show_in_nav_menus' => true,
                'supports' => array(
                    'title',
                    'thumbnail',
                    'author',
                    'editor',
                    'excerpt',
                    'comments',
                ),
                'show_ui' => true,
                'show_in_menu' => true,
                'menu_icon' => AIT_FRAMEWORK_URL . '/CustomTypes/dir-item/dir-item.png',
                'menu_position' => $GLOBALS['aitThemeCustomTypes']['dir-item'],
                'has_archive' => true,
                'query_var' => 'dir-item',
                'rewrite' => array('slug' => 'item'),
                'capability_type' => 'ait-dir-item',
                'map_meta_cap' => true
            )
        );
        aitDirItemTaxonomies();

        flush_rewrite_rules(false);
    }

add_action( 'init', 'aitDirItemPostType');

I want to change my custom post URL. I want to remove "item" from URL.

With the below code and with custom post type url plugin http://wordpress.org/plugins/custom-post-type-permalinks/ my URL is:

Original URL : http://mywebsite/wp/item/america/mayer/

Want to change it to http://mywebsite/wp/america/mayer/

Without using htaccess.

WordPress Code

    function aitDirItemPostType() {

        register_post_type( 'ait-dir-item',
            array(
                'labels' => array(
                    'name'          => 'Items',
                    'singular_name' => 'Item',
                    'add_new'       => 'Add new',
                    'add_new_item'  => 'Add new item',
                    'edit_item'     => 'Edit item',
                    'new_item'      => 'New item',
                    'not_found'     => 'No items found',
                    'not_found_in_trash' => 'No items found in Trash',
                    'menu_name'     => 'Items',
                ),
                'description' => 'Manipulating with items',
                'public' => true,
                'show_in_nav_menus' => true,
                'supports' => array(
                    'title',
                    'thumbnail',
                    'author',
                    'editor',
                    'excerpt',
                    'comments',
                ),
                'show_ui' => true,
                'show_in_menu' => true,
                'menu_icon' => AIT_FRAMEWORK_URL . '/CustomTypes/dir-item/dir-item.png',
                'menu_position' => $GLOBALS['aitThemeCustomTypes']['dir-item'],
                'has_archive' => true,
                'query_var' => 'dir-item',
                'rewrite' => array('slug' => 'item'),
                'capability_type' => 'ait-dir-item',
                'map_meta_cap' => true
            )
        );
        aitDirItemTaxonomies();

        flush_rewrite_rules(false);
    }

add_action( 'init', 'aitDirItemPostType');
Share Improve this question edited May 15, 2014 at 12:56 Aravona 5851 gold badge5 silver badges19 bronze badges asked May 15, 2014 at 11:55 JohanJohan 14 bronze badges 2
  • 1 "item" is the basename for your custom post type. You can change it in this line of code 'rewrite' => array('slug' => 'item'), but you can't remove it. If you change it, remember you need to flush the rewrite rules afterwards by visiting the permalinks page in the administrative dashboard. – epilektric Commented May 15, 2014 at 13:12
  • no this is a theme code.and if i will change according to you then url will use air-dir-item in place of item. so another solution please.. – Johan Commented May 15, 2014 at 13:22
Add a comment  | 

2 Answers 2

Reset to default 0

You can omit the "base", by adding

rewrite => array(
    slug       => 'item',
    with_front => false,
),

Don't forget to flush your rewrite rules after you changed that. The easiest way to do so it to simply visit the "admin > settings > permalinks" page.

If you need to add that to an already registered post type, then take a look at the internals of register_post_type() and the available hooks and filters in its source Link to GitHub: Make use of the registered_post_type action which comes with two arguments: $post_type and $args.

I think you have to set the slug value to an empty string like '', deleting item.

I think that @kaiser think that item is a generic string for the posts, and he suggest to not prepend that string to your custom post type permalink. That's a good idea. But item is the slug for the CPT, so that's why is not working

本文标签: pluginscustom url with custom post