0

I'm trying to edit an existing custom post type for the theme I'm using called "Divi". This custom post type is called a "project". Now I'm trying to change the default slug of:

http://example.com/project/name_of_a_project

to

http://example.com/some_category/name_of_a_project

I got this to work with the next code:

function custom_post_name () {
    return array(
        'feeds' => true,
        'slug' => '%category%',
        'with_front' => false
    );
}
add_filter( 'et_project_posttype_rewrite_args', 'custom_post_name' );

function custom_project_post_link( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) && $post->post_type == "project" ){
        $terms = wp_get_object_terms( $post->ID, 'project_category' );
        if( $terms){
            echo "hei";
            return str_replace( '%category%' , $terms[0]->slug , $post_link );
        }else{
            return str_replace( '/%category%' , '' , $post_link );
        }
    }
    return $post_link;  
}
add_filter( 'post_type_link', 'custom_project_post_link', 1, 3 );

But now all the other post types return a "page not found" if I try to visit them. I suppose it maybe got to do with rewrite rules but I couldn't find anything that worked for me yet.

  • You can try to visit the "permalinks" page under "settings > permalinks". Simply by visiting the page, it will flush the rewrite rules and reinitiate it. Maaaybe this helps – user3135691 Jun 28 '17 at 9:01
  • Have been doing that after every change. – ForsakenCreator Jun 28 '17 at 9:05

Your Answer

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

Browse other questions tagged or ask your own question.