Change the url of Projects in Divi Theme

For the taxonomies (like category or any other custom taxonomies, registered for the custom post type, using register_taxonomy function ), to be part of single post permalinks in wordpress, you may use the following code in your functions.php of the current theme:

function my_post_type_link( $post_link, $post ){
    if( !empty( $post->post_type ) && in_array( $post->post_type, array( 'projects' ) ) ){
        $slug = $post->post_name;
        $post_id = $post->ID;
        $post_cats = get_the_terms( $post_id, 'category' );     
        if( !empty( $post_cats[0] ) ){
            $target_category = $post_cats[0];
            while( !empty( $target_category->slug ) ){
                $slug = $target_category->slug . "https://wordpress.stackexchange.com/" . $slug;
                if( !empty( $target_category->parent ) ){
                    $target_category = get_term( $target_category->parent, 'category' );
                }else{
                    break;
                }
            }
            $post_link = get_option( 'home' ) . "https://wordpress.stackexchange.com/". urldecode( $slug );
        }
    }
    return  $post_link;
}
add_filter( 'post_type_link', 'my_post_type_link', 10, 2 );

function my_pre_get_posts( $q ){     
    if( $q->is_main_query() && !is_admin() && $q->is_single ){
        $q->set( 'post_type',  array_merge( array( 'post' ), array( 'projects' ) )   );
    }
    return $q;
}
add_action( 'pre_get_posts', 'my_pre_get_posts', 10 );

Using this, you may have the desired output, for your projects posts’ permalinks.

Leave a Comment