Change the url of Projects in Divi Theme

I use the Divi Theme for a WordPress website. At the moment, everything in Projects Custom Post, have the same url in the following format:

www.mywebsite.com/projects/project-name

What I tried to do is to change the projects to a more dynamic name based on the category a project belongs. I know that I can just create a few more custom posts, but I prefer to keep it like this.

I tried to change the permalink settings to /%category%/ but then my projects didn’t work with either of the two versions.

I tried also this way, but it just changes to a new static name, without the category dynamic url:

<?php
function custom_post_name () {
return array(
'feeds' => true,
'slug' => 'anewname',
'with_front' => false,
);
}
add_filter( 'et_project_posttype_rewrite_args', 'custom_post_name' );
?>

Is there any advice to achieve this or I should turn to different custom posts?

Example:

I have two projects (a custom post type of divi theme) with the categories: foods and drinks. At the moment, the url of them are the following

  1. www.mydomain.com/projects/foodexample1
  2. www.mydomain.com/projects/drinkexample2

I want to make them as:

  1. www.mydomain.com/foods/foodexample1
  2. www.mydomain.com/drinks/drinkexample2

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 . '/' . $slug;
                if( !empty( $target_category->parent ) ){
                    $target_category = get_term( $target_category->parent, 'category' );
                }else{
                    break;
                }
            }
            $post_link = get_option( 'home' ) . '/'. 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x