admin管理员组文章数量:1331083
I've created a custom post type "testimonials" -- works fine, except I want the url to be "domain/results/testimonials" instead of "domain/testimonials"
Do I just need to change something in the function that creates the custom post type? Or is there something else I need to do?
Thanks, Scott
I've created a custom post type "testimonials" -- works fine, except I want the url to be "domain/results/testimonials" instead of "domain/testimonials"
Do I just need to change something in the function that creates the custom post type? Or is there something else I need to do?
Thanks, Scott
Share Improve this question asked Jul 13, 2020 at 18:13 ScottMScottM 455 bronze badges2 Answers
Reset to default 2When you register the CPT, use the rewrite
argument to add the prefix.
For example if your CPT is currently registered like this:
<?php
function wpse_create_cpt_testimonial() {
$args = array(
'public' => true,
'label' => __( 'Testimonials', 'textdomain' ),
// ... you probably have other arguments as well
);
register_post_type( 'testimonial', $args );
}
add_action( 'init', 'wpse_create_cpt_testimonial' );
?>
you can add rewrite
like this:
<?php
function wpse_create_cpt_testimonial() {
$args = array(
'public' => true,
'label' => __( 'Testimonials', 'textdomain' ),
// Here's the part where you're changing the URL
'rewrite' => array('slug' => 'results/testimonials'),
// If you want that URL to be an archive, add this line too
'has_archive' => 'results/testimonials',
// ... keep your other arguments as well
);
register_post_type( 'testimonial', $args );
}
add_action( 'init', 'wpse_create_cpt_testimonial' );
?>
If this doesn't change things immediately, you may need to run unregister_post_type('testimonial')
right before re-registering it, and/or visit the Permalinks page to flush rewrite rules.
A little more time searching around, and I found the answer... here it is in case it helps anyone else.
In the function that creates the custom post type, I added this:
$rewrite = array(
'slug' => 'results/testimonials',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
So the full custom post type code is now:
/*
* Creating a function to create our CPT
*/
static function testimonial_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Testimonials', 'Post Type General Name'),
'singular_name' => _x( 'Testimonial', 'Post Type Singular Name'),
'menu_name' => __( 'Testimonials'),
'parent_item_colon' => __( 'Parent Testimonial'),
'all_items' => __( 'All Testimonials'),
'view_item' => __( 'View Testimonial'),
'add_new_item' => __( 'Add New Testimonial'),
'add_new' => __( 'Add New'),
'edit_item' => __( 'Edit Testimonial'),
'update_item' => __( 'Update Testimonial'),
'search_items' => __( 'Search Testimonial'),
'not_found' => __( 'Not Found'),
'not_found_in_trash' => __( 'Not found in Trash'),
);
$rewrite = array(
'slug' => 'results/testimonials',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'testimonials'),
'description' => __( 'Client testimonials'),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields','page-attributes' ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'feature_on','service','industry' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'menu_icon' => "dashicons-thumbs-up",
'rewrite' => $rewrite,
"rest_base" => "Testimonial",
"rest_controller_class" => "WP_REST_Posts_Controller",
'show_in_rest' => true,
"query_var" => true,
);
// Registering your Custom Post Type
register_post_type( 'testimonials', $args );
}
I hope that helps someone else!
本文标签: permalinksCustom post typehow can I make it a suburl of another page
版权声明:本文标题:permalinks - Custom post type - how can I make it a sub-url of another page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742265477a2443310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论