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
- www.mydomain/projects/foodexample1
- www.mydomain/projects/drinkexample2
I want to make them as:
- www.mydomain/foods/foodexample1
- 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
- www.mydomain/projects/foodexample1
- www.mydomain/projects/drinkexample2
I want to make them as:
- www.mydomain/foods/foodexample1
- www.mydomain/drinks/drinkexample2
- 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
1 Answer
Reset to default 0For 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.