简体   繁体   中英

wordpress custom post type slugs and rewrite

I have 2 custom post types in my wordpress site,

 // Our custom post type function
function create_posttype() {
 
    register_post_type( 'properties',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Properties' ),
                'singular_name' => __( 'Property' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'properties'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

and

 // Our custom post type function
function create_team_posttype() {
 
    register_post_type( 'team',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Team Members' ),
                'singular_name' => __( 'Team Member' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'our-team'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_team_posttype' );

When I go to mydomain.com/properties I see my archive-properties.php page template I see the correct template and content. I have a team archive teamplate called archive-team.php but but when I go to it's URl mydomain.com/our-team I see the archive-properties.php template am I doing something incorrect?

You'll need to pass an argument for the archive slug. Currently, you are only passing the slug for the single custom post. So your custom posts are now at /our-team/some-team-slug

If you want to modify your archive, you'll need to use:

$args = array(
  'has_archive' => 'our-team',
);

In the codex it is explained as follows :

'has_archive' (bool|string) Whether there should be post type archives, or if a string, the archive slug to use. Will generate the proper rewrite rules if $rewrite is enabled. Default false.

You might need to rewrite your permalinks or unregister and re-register your custom post type to be sure the change gets picked up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM