最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Change the url of Projects in Divi Theme

programmeradmin1浏览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/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/projects/foodexample1
  2. www.mydomain/projects/drinkexample2

I want to make them as:

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

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/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/projects/foodexample1
  2. www.mydomain/projects/drinkexample2

I want to make them as:

  1. www.mydomain/foods/foodexample1
  2. www.mydomain/drinks/drinkexample2
Share Improve this question edited Dec 21, 2015 at 8:04 Tasos asked Dec 20, 2015 at 17:13 TasosTasos 1693 silver badges13 bronze badges 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 – WordPress Speed Commented Dec 21, 2015 at 4:10
  • @Trix Added to the question – Tasos Commented Dec 21, 2015 at 8:04
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论