admin管理员组文章数量:1125557
I need the following url structures for my Custom Posttype and Custom Taxonomy. Can anyone please help in achieving them? Spent a lot of time but failed. Ton of thanks in advance.
- Post URL: / (Achieved)
- Post Archive: / (Achieved)
- Term URL: / (Need this)
Here's my setup
add_action( 'init', 'custom_post_type_intervention' );
function custom_post_type_intervention(){
$labels = array(
'name' => 'Interventions',
'singular_name' => 'Intervention',
'add_new' => 'Add Intervention',
'add_new_item' => 'New Intervention',
'edit_item' => 'Edit Intervention',
'all_items' => 'All Interventions',
'menu_name' => 'Interventions'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'query_var' => true,
'taxonomies' => array( 'intervention_cat' ),
'has_archive' => 'interventions',
'show_in_nav_menus' => true,
'menu_position' => 6,
'menu_icon' => 'dashicons-rest-api',
'supports' => array( 'title', 'editor', 'author', 'revisions' ),
'rewrite' => array( 'slug' => 'interventions/%intvn-cat%', 'with_front' => false )
);
register_post_type( 'intervention', $args );
unset( $args, $labels );
$labels = array(
'name' => 'Categories',
'singular_name' => 'Category'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => 'interventions', 'with_front' => false ),
'default_term' => array( 'name' => 'Uncategorized', 'slug' => 'uncategorized' )
);
register_taxonomy( 'intervention_cat', 'intervention', $args );
unset( $args, $labels );
function intervention_custom_post_slug( $post_link, $post ){
if( 'intervention' === $post->post_type ){
$terms = wp_get_object_terms( $post->ID, 'intervention_cat' );
if( $terms ){
return str_replace( '%intvn-cat%', $terms[0]->slug, $post_link );
}else{
return str_replace( '%intvn-cat%', 'uncategorized', $post_link );
}
}
return $post_link;
}
add_rewrite_tag( '%intvn-cat%', '(.+)' );
add_filter( 'post_type_link', 'intervention_custom_post_slug', 1, 3 );
//add_rewrite_rule( '^interventions/(.*)/?$', 'index.php?intervention_cat=$matches[1]', 'top' );
}
I need the following url structures for my Custom Posttype and Custom Taxonomy. Can anyone please help in achieving them? Spent a lot of time but failed. Ton of thanks in advance.
- Post URL: https://www.domain.com/interventions/taxonomy-slug/post-slug/ (Achieved)
- Post Archive: https://www.domain.com/interventions/ (Achieved)
- Term URL: https://www.domain.com/interventions/taxonomy-slug/ (Need this)
Here's my setup
add_action( 'init', 'custom_post_type_intervention' );
function custom_post_type_intervention(){
$labels = array(
'name' => 'Interventions',
'singular_name' => 'Intervention',
'add_new' => 'Add Intervention',
'add_new_item' => 'New Intervention',
'edit_item' => 'Edit Intervention',
'all_items' => 'All Interventions',
'menu_name' => 'Interventions'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'query_var' => true,
'taxonomies' => array( 'intervention_cat' ),
'has_archive' => 'interventions',
'show_in_nav_menus' => true,
'menu_position' => 6,
'menu_icon' => 'dashicons-rest-api',
'supports' => array( 'title', 'editor', 'author', 'revisions' ),
'rewrite' => array( 'slug' => 'interventions/%intvn-cat%', 'with_front' => false )
);
register_post_type( 'intervention', $args );
unset( $args, $labels );
$labels = array(
'name' => 'Categories',
'singular_name' => 'Category'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => 'interventions', 'with_front' => false ),
'default_term' => array( 'name' => 'Uncategorized', 'slug' => 'uncategorized' )
);
register_taxonomy( 'intervention_cat', 'intervention', $args );
unset( $args, $labels );
function intervention_custom_post_slug( $post_link, $post ){
if( 'intervention' === $post->post_type ){
$terms = wp_get_object_terms( $post->ID, 'intervention_cat' );
if( $terms ){
return str_replace( '%intvn-cat%', $terms[0]->slug, $post_link );
}else{
return str_replace( '%intvn-cat%', 'uncategorized', $post_link );
}
}
return $post_link;
}
add_rewrite_tag( '%intvn-cat%', '(.+)' );
add_filter( 'post_type_link', 'intervention_custom_post_slug', 1, 3 );
//add_rewrite_rule( '^interventions/(.*)/?$', 'index.php?intervention_cat=$matches[1]', 'top' );
}
Share
Improve this question
asked Jan 31, 2024 at 3:26
TechieBoyTechieBoy
154 bronze badges
2 Answers
Reset to default 0There are 2 possible ways to fix the issue with the term's permalinks (which is currently using the wrong query var name – intvn-cat
):
Easy. Just set the 3rd parameter for
add_rewrite_tag()
tointervention_cat=
. I.e.add_rewrite_tag( '%intvn-cat%', '(.+)', 'intervention_cat=' );
This is what I would actually do – Remove the
add_rewrite_tag()
part in your code, then replace all of the%intvn-cat%
instances with%intervention_cat%
.
And then, be sure to flush the rewrite rules by simply visiting the permalink settings admin page.
Additionally, for performance reasons, functions like get_the_terms()
(which the results of has been cached), should be used. See https://developer.wordpress.org/reference/functions/wp_get_object_terms/#more-information.
To achieve the desired Term URL structure (https://www.domain.com/interventions/taxonomy-slug/)
for your custom taxonomy in WordPress, you'll need to adjust your rewrite rules and ensure that your taxonomy is correctly set up to handle these URLs. From the code you've provided, it looks like you're on the right track, but you need to uncomment and possibly adjust the rewrite rule for the taxonomy.
Here's what you need to do:
Uncomment the Rewrite Rule: Uncomment the add_rewrite_rule
line in your code. This rule is necessary for WordPress to understand how to handle URLs that include your custom taxonomy.
Adjust the Rewrite Rule: Ensure that the rewrite rule correctly captures the taxonomy slug and maps it to the correct query variable. The current rule looks like it should work, but you might need to adjust it based on your exact requirements.
Flush Rewrite Rules: After making changes to rewrite rules, it's important to flush the rewrite rules to ensure that WordPress recognizes the new URL structure. You can do this by visiting the Settings > Permalinks page in your WordPress admin area and simply clicking "Save Changes".
Taxonomy Query Variable: Make sure that the query variable you're using (intervention_cat
) matches the one registered with your taxonomy.
Here's the adjusted part of your code with the rewrite rule uncommented:
add_action( 'init', 'custom_post_type_intervention' );
function custom_post_type_intervention(){
// ... [rest of your code]
add_rewrite_tag( '%intvn-cat%', '(.+)' );
add_filter( 'post_type_link', 'intervention_custom_post_slug', 1, 3 );
add_rewrite_rule( '^interventions/([^/]+)/?$', 'index.php?intervention_cat=$matches[1]', 'top' );
}
In this rewrite rule, ^interventions/([^/]+)/?$
captures any URL that starts with interventions/
followed by any character sequence that is not a slash (/
), then optionally ends (the /?$
part). The captured part is then passed to the intervention_cat
query variable.
Remember, any time you change rewrite rules, visit the Permalinks settings page and save the changes to flush the rules. If the URLs still don't work as expected, you may need to debug your setup further, checking for conflicts with other plugins or themes, or issues in your code.
本文标签: custom post typesRewrite taxonomy permalink appended to CPT archive url
版权声明:本文标题:custom post types - Rewrite taxonomy permalink appended to CPT archive url 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736668486a1946791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论