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

I’ve created a custom post type “testimonials” — works fine, except I want the url to be “domain.com/results/testimonials” instead of “domain.com/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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x