admin管理员组文章数量:1289845
Question 1 Permalink structure
I trying to implement the right url/permalink structure for my site, but I can't get it to work.
I have the following setup:
Custom post type: training
register_post_type( 'training',
array(
'labels' => array(
'name' => __( 'Trainingen' ),
'singular_name' => __( 'Training' ),
'all_items' => __( 'Alle trainingen' ),
'view_item' => __( 'Bekijk training' ),
'add_new_item' => __( 'Nieuwe training' ),
'add_new' => __( 'Nieuwe training' )
),
'public' => true,
'has_archive' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'our-training', 'with_front' => false),
'show_in_rest' => true
)
);
**Custom taxonomy : training-category**
$args = array(
"label" => ( "training category"),
"labels" => $labels,
"rewrite" => array( 'slug' => 'training-category', 'with_front' => true, ),
"show_admin_column" => true,
"hierarchical" => false,
"has_archive" => true,
"show_ui" => true
);
register_taxonomy( "training_category", array( "event", "post", "training", "employee"), $args );
The permalink structure I want to establish:
our-training/training-category-1/training-name-1
our-training/training-category-2/training-name-2
our-training/training-category-1/training-name-3
what I do get is:
our-training/training-name-1
our-training/training-name-2
our-training/training-name-3
So the custom taxonomy is not inserted correctly in the permalink.
(I am also using Yoast breadcrumbs and have set the taxonomy to show for the training cpt to: training-category (my custom tax).)
Any tips on how I can get the desired permalink structure?
Question 2 - Redirect
Secondly I use facetwp for filtering so I need to redirect the category archive to a facetwp archive page. Redirect I want to establish (simplyfied):
our-training/training-category-1
must redirect to:
our-training?cat=training-category-1
How can I do this the best way?
Any pointers in the right direction will be highly appreciated!
Question 1 Permalink structure
I trying to implement the right url/permalink structure for my site, but I can't get it to work.
I have the following setup:
Custom post type: training
register_post_type( 'training',
array(
'labels' => array(
'name' => __( 'Trainingen' ),
'singular_name' => __( 'Training' ),
'all_items' => __( 'Alle trainingen' ),
'view_item' => __( 'Bekijk training' ),
'add_new_item' => __( 'Nieuwe training' ),
'add_new' => __( 'Nieuwe training' )
),
'public' => true,
'has_archive' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'our-training', 'with_front' => false),
'show_in_rest' => true
)
);
**Custom taxonomy : training-category**
$args = array(
"label" => ( "training category"),
"labels" => $labels,
"rewrite" => array( 'slug' => 'training-category', 'with_front' => true, ),
"show_admin_column" => true,
"hierarchical" => false,
"has_archive" => true,
"show_ui" => true
);
register_taxonomy( "training_category", array( "event", "post", "training", "employee"), $args );
The permalink structure I want to establish:
our-training/training-category-1/training-name-1
our-training/training-category-2/training-name-2
our-training/training-category-1/training-name-3
what I do get is:
our-training/training-name-1
our-training/training-name-2
our-training/training-name-3
So the custom taxonomy is not inserted correctly in the permalink.
(I am also using Yoast breadcrumbs and have set the taxonomy to show for the training cpt to: training-category (my custom tax).)
Any tips on how I can get the desired permalink structure?
Question 2 - Redirect
Secondly I use facetwp for filtering so I need to redirect the category archive to a facetwp archive page. Redirect I want to establish (simplyfied):
our-training/training-category-1
must redirect to:
our-training?cat=training-category-1
How can I do this the best way?
Any pointers in the right direction will be highly appreciated!
Share Improve this question asked Jul 8, 2021 at 10:04 thegirlinthecafethegirlinthecafe 561 silver badge9 bronze badges 2 |1 Answer
Reset to default 1There was a problem with archive page and pagination in the original answer. Therefore, instead of changing the "rewrite" => ['slug' => 'our-training/%training-category%']
in register_post_type()
as I suggested earlier, add the following rewrite rules and change the links created for CPT.
The rules will handle such addresses:
- display single
training
post with pagination support
/our-training/training-category-1/training-name/
/our-training/training-category-1/training-name/2/
- display
training
posts assigned totraining-category-1
(uncomment first rewrite rule) note: this is query for taxonomy term, not cpt, so the first available template from the list will be used:taxonomy-training_category.php
>taxonomy.php
>archive.php
/our-training/training-category-1/
/our-training/training-category-1/page/2/
Default rules will handle:
- display all
training
posts (archive)
/our-training/
- display all posts (any type, "event", "employee", etc.) assigned to
training-category-1
/training-category/training-category-1/
This code worked for me.
add_action( 'init', 'se391612_cpt_permalink', 20 );
add_filter( 'post_type_link', 'se391612_term_in_cpt_link', 20, 4 );
function se391612_cpt_permalink()
{
//
// CPT posts from custom taxonomy term (term archive)
//add_rewrite_rule(
// 'our-training/([^/]+)(?:/page/?([0-9]+))?/?$',
// 'index.php?post_type=training&training_category=$matches[1]&paged=$matches[2]',
// 'top'
//);
//
// single post with pagination
add_rewrite_rule(
'our-training/([^/]+)/([^/]+)(?:/([0-9]+))?/?$',
'index.php?post_type=training&training=$matches[2]&page=$matches[3]',
'top'
);
}
function se391612_term_in_cpt_link( $post_link, $post, $leavename, $sample )
{
if ( ! $post instanceof \WP_Post || 'training' != $post->post_type)
return $post_link;
$new_link = $post_link;
$all_terms = wp_get_post_terms( $post->ID, 'training_category', [
'hide_empty' => '0',
'fields' => 'slugs',
'orderby' => 'term_id',
] );
if ( !is_array($all_terms) || 0 == count($all_terms) )
return $post_link;
$term = array_shift($all_terms) . '/';
$new_link = str_replace('our-training/', 'our-training/'.$term, $new_link);
return $new_link;
}
Important note:
If you uncomment the first add_rewrite_rule()
to support /our-training/training-category-1
permalink format,
an address collision will occur. For WP there is no difference between these addresses,
how it interprets them depends on the order of rewrite rules.
/our-training/training-category-1 // all "training" post from category "training-category-1"
/our-training/some-training-name // single "training" post
However, if you would like both address types to work and you know that there will be only several categories, you can add these categories in the rules (manually or automatically).
Reference:
register_post_type()
wp_get_post_terms()
post_type_link
filter
本文标签: Permalink problems with custom post type and custom taxonomy
版权声明:本文标题:Permalink problems with custom post type and custom taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741451529a2379513.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
with_front
key to TRUE in your CPT arguments. That should help with question 1 – Nathan Powell Commented Jul 8, 2021 at 10:46