• Resolved htausch

    (@htausch)


    I am trying to change the slug for a CPT in my child theme. I changed portfolio to homepage in my child but my permalink still shows site.com/portfolio/page.html. I want to change it to show site.com/homepage/page.html.

    This is the code in my functions.php file I am using to rename the CPT:

    add_action( 'wp_loaded', 'wpse_19240_change_home_labels', 20 );
    
    function wpse_19240_change_home_labels()
    {
        global $wp_post_types;
        $b = 'portfolio';
    
        if ( empty ( $wp_post_types[ $b ] )
            or ! is_object( $wp_post_types[ $b ] )
            or empty ( $wp_post_types[ $b ]->labels )
            )
            return;
    
        $wp_post_types[ $b ]->labels->name               = 'Sliders';
        $wp_post_types[ $b ]->labels->singular_name      = 'Slider';
        $wp_post_types[ $b ]->labels->add_new_item       = 'Add New Slider';
        $wp_post_types[ $b ]->labels->edit_item          = 'Edit Slider';
        $wp_post_types[ $b ]->labels->new_item           = 'New Slider';
        $wp_post_types[ $b ]->labels->view_item          = 'View Slider';
        $wp_post_types[ $b ]->labels->search_items       = 'Search Sliders';
        $wp_post_types[ $b ]->labels->not_found          = 'No sliders found.';
        $wp_post_types[ $b ]->labels->not_found_in_trash = 'No sliders found in Trash.';
        $wp_post_types[ $b ]->labels->menu_name          = 'Homepage Sliders';
        $wp_post_types[ $b ]->labels->all_items          = 'All Sliders';
        $wp_post_types[ $b ]->labels->add_new            = 'Add New Slider';
        $wp_post_types[ $b ]->labels->name_admin_bar     = 'Slider';
    }

    EDIT: So Googling, I found some code that rewrites the slug successfully, but now I get a 404 page when I navigate to site.com/homepage/page.html. I made sure to try and change my file name from single-portfolio.php to single-homepage.php. Any other suggestions?

    function change_slug_of_post_type_portfolio()
    {
        register_post_type('portfolio', array(
            'rewrite' => array (
               'slug' => 'homepage',
            )
        )
    );
    }
    add_action('init', 'change_slug_of_post_type_portfolio', 20);

Viewing 1 replies (of 1 total)
  • Thread Starter htausch

    (@htausch)

    SOLUTION:

    flush_rewrite_rules(); 
    
    function change_slug_of_post_type_portfolio() {
        $args = get_post_type_object("portfolio");
    	$args->rewrite["slug"] = "homepage";
    	register_post_type($args->name, $args);
    }
    add_action('init', 'change_slug_of_post_type_portfolio', 20);

Viewing 1 replies (of 1 total)
  • The topic ‘Change slug when changing custom post type in child’ is closed to new replies.