0

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
3
  • You need to do a little extra work to use taxonomy terms in CPT URLs, see this answer.
    – Milo
    Commented Dec 20, 2015 at 17:49
  • What is your desired output? please explain in clear examples what do u expect to have Commented Dec 21, 2015 at 4:10
  • @Trix Added to the question
    – Tasos
    Commented Dec 21, 2015 at 8:04

1 Answer 1

0

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.

1
  • I will check it later today and let you know :)
    – Tasos
    Commented Dec 21, 2015 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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