最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Custom post type - how can I make it a sub-url of another page?

programmeradmin4浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 2

When 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!

发布评论

评论列表(0)

  1. 暂无评论