0

I'm using Read WP (premium) with childthime. This theme have Portfolio CPT (slug: potfolio) declared in functions.php. Each project can be nested in a Department (slug: department). If i want to see only one specific department, the URL will be something like this:

mysite.com/department/category-name.

If I want to see one specific projet, the URL will be:

mysite.com/portfolio/my-projects-name.

The question is: How can I change these two slugs? I need change:

  • porfolio -> conteudo
  • department -> material

I already found in code where this slug are registred, but I have no idea how to rewhrite this.

Here is the code snippet for portfolio (parent theme functions.php):

function create_post_type_portfolio()
    {
        $labels = array('name' => __( 'Portfolio', 'read' ),
                        'singular_name' => __( 'Portfolio Item', 'read' ),
                        'add_new' => __( 'Add New', 'read' ),
                        'add_new_item' => __( 'Add New', 'read' ),
                        'edit_item' => __( 'Edit', 'read' ),
                        'new_item' => __( 'New', 'read' ),
                        'all_items' => __( 'All', 'read' ),
                        'view_item' => __( 'View', 'read' ),
                        'search_items' => __( 'Search', 'read' ),
                        'not_found' =>  __( 'No Items found', 'read' ),
                        'not_found_in_trash' => __( 'No Items found in Trash', 'read' ),
                        'parent_item_colon' => '',
                        'menu_name' => 'Portfolio' );

        $args = array(  'labels' => $labels,
                        'public' => true,
                        'exclude_from_search' => false,
                        'publicly_queryable' => true,
                        'show_ui' => true,
                        'query_var' => true,
                        'show_in_nav_menus' => true,
                        'capability_type' => 'post',
                        'hierarchical' => false,
                        'menu_position' => 5,
                        'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions' ),
                        'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ));

        register_post_type( 'portfolio' , $args );
    }

And the code snippet for departments (parent theme functions.php):

function portfolio_taxonomy()
    {
        $labels_dep = array('name' => __( 'Departments', 'read' ),
                            'singular_name' => __( 'Department', 'read' ),
                            'search_items' =>  __( 'Search', 'read' ),
                            'all_items' => __( 'All', 'read' ),
                            'parent_item' => __( 'Parent Department', 'read' ),
                            'parent_item_colon' => __( 'Parent Department:', 'read' ),
                            'edit_item' => __( 'Edit', 'read' ),
                            'update_item' => __( 'Update', 'read' ),
                            'add_new_item' => __( 'Add New', 'read' ),
                            'new_item_name' => __( 'New Department Name', 'read' ),
                            'menu_name' => __( 'Departments', 'read' ) );

        register_taxonomy(  'department',
                            array( 'portfolio' ),
                            array( 'hierarchical' => true,
                            'labels' => $labels_dep,
                            'show_ui' => true,
                            'public' => true,
                            'query_var' => true,
                            'rewrite' => array( 'slug' => 'content' ) ) );


        $labels_tag = array('name' => __( 'Portfolio Tags', 'read' ),
                            'singular_name' => __( 'Portfolio Tag', 'read' ),
                            'search_items' =>  __( 'Search', 'read' ),
                            'all_items' => __( 'All', 'read' ),
                            'parent_item' => __( 'Parent Portfolio Tag', 'read' ),
                            'parent_item_colon' => __( 'Parent Portfolio Tag:', 'read' ),
                            'edit_item' => __( 'Edit', 'read' ),
                            'update_item' => __( 'Update', 'read' ),
                            'add_new_item' => __( 'Add New', 'read' ),
                            'new_item_name' => __( 'New Portfolio Tag Name', 'read' ),
                            'menu_name' => __( 'Portfolio Tags', 'read' ) );

        register_taxonomy(  'portfolio_tags',
                            array( 'portfolio' ),
                            array( 'hierarchical' => false,
                            'labels' => $labels_tag,
                            'show_ui' => true,
                            'public' => true,
                            'query_var' => true,
                            'rewrite' => array( 'slug' => 'portfolio_tags' ) ) );
    }
    // end portfolio_taxonomy

So, how can I overrride this code without editing parent theme functions?

1

WordPress 4.5 introduced the functions unregister_post_type() and unregister_taxonomy(). You could deregister your post type respectively taxonomy and register them again with your custom settings. The post type is still named portfolio. You could rename it, but you would probably have to check your template files for references.

add_action('init', 'wpse_247924_overwrite_theme_post_types', 1000);
function wpse_247924_overwrite_theme_post_types() {

    unregister_post_type( 'portfolio' );

    $labels = array(
        'name' => __( 'Portfolio', 'read' ),
        'singular_name' => __( 'Portfolio Item', 'read' ),
        'add_new' => __( 'Add New', 'read' ),
        'add_new_item' => __( 'Add New', 'read' ),
        'edit_item' => __( 'Edit', 'read' ),
        'new_item' => __( 'New', 'read' ),
        'all_items' => __( 'All', 'read' ),
        'view_item' => __( 'View', 'read' ),
        'search_items' => __( 'Search', 'read' ),
        'not_found' =>  __( 'No Items found', 'read' ),
        'not_found_in_trash' => __( 'No Items found in Trash', 'read' ),
        'parent_item_colon' => '',
        'menu_name' => 'Portfolio'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'show_in_nav_menus' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 5,
        'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions' ),
        'rewrite' => array(
            'slug' => 'conteudo',
            'with_front' => false
        )
    );

    register_post_type( 'portfolio' , $args );

    unregister_taxonomy( 'department' );

    $labels_dep = array(
        'name' => __( 'Departments', 'read' ),
        'singular_name' => __( 'Department', 'read' ),
        'search_items' =>  __( 'Search', 'read' ),
        'all_items' => __( 'All', 'read' ),
        'parent_item' => __( 'Parent Department', 'read' ),
        'parent_item_colon' => __( 'Parent Department:', 'read' ),
        'edit_item' => __( 'Edit', 'read' ),
        'update_item' => __( 'Update', 'read' ),
        'add_new_item' => __( 'Add New', 'read' ),
        'new_item_name' => __( 'New Department Name', 'read' ),
        'menu_name' => __( 'Departments', 'read' )
    );

    register_taxonomy(
        'material',
        array( 'portfolio' ),
        array(
            'hierarchical' => true,
            'labels' => $labels_dep,
            'show_ui' => true,
            'public' => true,
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'material'
            )
        )
    );


    $labels_tag = array(
        'name' => __( 'Portfolio Tags', 'read' ),
        'singular_name' => __( 'Portfolio Tag', 'read' ),
        'search_items' =>  __( 'Search', 'read' ),
        'all_items' => __( 'All', 'read' ),
        'parent_item' => __( 'Parent Portfolio Tag', 'read' ),
        'parent_item_colon' => __( 'Parent Portfolio Tag:', 'read' ),
        'edit_item' => __( 'Edit', 'read' ),
        'update_item' => __( 'Update', 'read' ),
        'add_new_item' => __( 'Add New', 'read' ),
        'new_item_name' => __( 'New Portfolio Tag Name', 'read' ),
        'menu_name' => __( 'Portfolio Tags', 'read' )
    );

    register_taxonomy(
        'portfolio_tags',
        array( 'portfolio' ),
        array(
            'hierarchical' => false,
            'labels' => $labels_tag,
            'show_ui' => true,
            'public' => true,
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'portfolio_tags'
            )
        )
    );

}
  • Thank you for you tip, but can you give me an example? I read the codex, but I couldn't understand... – Gustavo Sartori Nov 30 '16 at 16:08
  • 1
    add_action('init', 'delete_old_post_types', 1000); function delete_post_types() { unregister_post_type('portfolio'); } This will unregister the portfolio post type, but I'm not sure about the taxonomy. – WebElaine Nov 30 '16 at 16:40
  • There is also a unregister_taxonomy() function. – luke Nov 30 '16 at 19:09
  • Hey @luke, I've just inserted this in my child theme functions.php and got this warning in a white screen (admin). In front-end all site loads correctly, but the warning is being showed. Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'wpse_247924_overwrite_theme_post_types' not found or invalid function name in .... – Gustavo Sartori Dec 1 '16 at 11:20
  • @Luke, thank you A LOT! I've fixed some things in your code, like the add_action and other misspelling 'departement' to 'department'. Now it's working just like I wish. Thank you man!! – Gustavo Sartori Dec 1 '16 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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