I have a CPT called teachers and a custom taxonomy called teacher type.
I'm using the code below to rewrite the url for the CPT and instead of being like: website/teachers/post1
it changes the slug to use the category: website/teacher-type/post1
This is working correctly but now I need to add another taxonomy (year) to the CPT and to the slug and I need it to be like this:
website/teacher-type/year/post1
The code I'm using is compatible with WPML.
This is it:
function resources_cpt_generating_rule($wp_rewrite) {
global $sitepress, $sitepress_settings;
$has_filter = remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
$auto_adjust_ids = $sitepress_settings['auto_adjust_ids'];
$sitepress_settings['auto_adjust_ids'] = 0;
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'teacher_types',
'hide_empty' => false,
) );
$post_type = 'teachers';
foreach ($terms as $term) {
$rules[ $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&name=$matches[1]';
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
if ( $has_filter ) {
add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
}
$sitepress_settings['auto_adjust_ids'] = $auto_adjust_ids;
}
add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule', 20);
function change_link( $permalink, $post ) {
if( $post->post_type == 'teachers' ) {
$resource_terms = get_the_terms( $post, 'teacher_types' );
$term_slug = '';
if( ! empty( $resource_terms ) ) {
foreach ( $resource_terms as $term ) {
// The featured resource will have another category which is the main one
$term_slug = $term->slug;
break;
}
}
$permalink = apply_filters( 'wpml_permalink', trailingslashit( get_home_url() ) . $term_slug . '/' . $post->post_name );
}
return $permalink;
}
add_filter('post_type_link',"change_link",10,2);
Any ideas on how to concatenate those two taxonomies in the url?
Thanks for your help!
I have a CPT called teachers and a custom taxonomy called teacher type.
I'm using the code below to rewrite the url for the CPT and instead of being like: website/teachers/post1
it changes the slug to use the category: website/teacher-type/post1
This is working correctly but now I need to add another taxonomy (year) to the CPT and to the slug and I need it to be like this:
website/teacher-type/year/post1
The code I'm using is compatible with WPML.
This is it:
function resources_cpt_generating_rule($wp_rewrite) {
global $sitepress, $sitepress_settings;
$has_filter = remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
$auto_adjust_ids = $sitepress_settings['auto_adjust_ids'];
$sitepress_settings['auto_adjust_ids'] = 0;
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'teacher_types',
'hide_empty' => false,
) );
$post_type = 'teachers';
foreach ($terms as $term) {
$rules[ $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&name=$matches[1]';
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
if ( $has_filter ) {
add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
}
$sitepress_settings['auto_adjust_ids'] = $auto_adjust_ids;
}
add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule', 20);
function change_link( $permalink, $post ) {
if( $post->post_type == 'teachers' ) {
$resource_terms = get_the_terms( $post, 'teacher_types' );
$term_slug = '';
if( ! empty( $resource_terms ) ) {
foreach ( $resource_terms as $term ) {
// The featured resource will have another category which is the main one
$term_slug = $term->slug;
break;
}
}
$permalink = apply_filters( 'wpml_permalink', trailingslashit( get_home_url() ) . $term_slug . '/' . $post->post_name );
}
return $permalink;
}
add_filter('post_type_link',"change_link",10,2);
Any ideas on how to concatenate those two taxonomies in the url?
Thanks for your help!
Share Improve this question asked Jun 11, 2019 at 19:44 xaifuxaifu 313 bronze badges1 Answer
Reset to default 0You need to update below line at where you have register a custom post type using register_post_type function.
'rewrite' => array('slug' => 'teachers/%cat%')
To change permalink dynamically of post type you have to add below code in functions.php file :
function change_link( $post_link, $id = 0 ) {
$post = get_post( $id );
if ( is_object( $post ) ) {
$terms = wp_get_object_terms( $post->ID, array('teacher-type','year') );
if ( $terms ) {
return str_replace( '%cat%', $terms[0]->slug, $post_link );
}
}
return apply_filters('wpml_permalink', $post_link );
}
add_filter( 'post_type_link', 'change_link', 1, 3 );
//load the template on the new generated URL otherwise you will get 404's the page
function generated_rewrite_rules() {
add_rewrite_rule(
'^teachers/(.*)/(.*)/?$',
'index.php?post_type=teachers&name=$matches[2]',
'top'
);
}
add_action( 'init', 'generated_rewrite_rules' );
After that, you need to flush rewrites permalinks, goto the wp-admin > Settings > permalinks. just update permalink setting using "Save Changes" button.
it'll return urls like below :
- website/teachers/teacher-type/post1
- website/teachers/year/post1
Thank you!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745410010a4626485.html
评论列表(0条)