0

Desired behavior:

Custom Post Type slug: researchers, students, collaborators
URL: http://domainexample.com/people/researchers, http://domainexample.com/people/students, http://domainexample.com/people/collaborators
Template File: archive-people.php

How should I setup my Custom Post Type to achieve this behavior?
And I wanted to do it without using Taxonomies.
Possible?

Tried to set the has_archive slug and the rewrite slug to people/researchers, and the posts are loaded although the loaded template file still falls to arquive.php.

Current 'researchers' Custom Post Type registration code:

$args = array(
    "label" => __( "Researchers", "" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "",
    "has_archive" => "people/researchers",
    "show_in_menu" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "people/researchers", "with_front" => true ),
    "query_var" => true,
    "supports" => array( "title" ),
);
register_post_type("researchers", $args);

Thank you

1 Answer 1

1

You can set any arbitrary path in the rewrite slug and has_archive arguments:

$args = array(
    'rewrite'     => array('slug' => 'people/researchers'),
    'has_archive' => 'people/researchers',
    // other args...
);

EDIT

Your post type slug is researchers, so WordPress will be looking for the file archive-researchers.php by default. If you want to force a different template, you can use the archive_template filter:

function wpd_researchers_archive_template( $archive_template ){
     if( is_post_type_archive( 'researchers' ) ){
          $archive_template = locate_template( 'archive-people.php' );
     }
     return $archive_template;
}
add_filter( 'archive_template', 'wpd_researchers_archive_template' );
9
  • I tried setting the rewrite slug, the archive slug and even the query_var but the template file loaded still falls to archive.php… (the posts are loaded though). Jan 29, 2018 at 3:40
  • 1
    add your current post type registration code to your question.
    – Milo
    Jan 29, 2018 at 4:51
  • I have updated my question with the registration code. Thank you for your attention. Jan 29, 2018 at 14:52
  • Your code doesn’t show the post type slug you pass to register_post_type, that is what will dictate the correct template file name.
    – Milo
    Jan 29, 2018 at 16:49
  • I'm passing the 'researchers' slug to register_post_type. I need it to be 'researchers' and not 'people' because I have other CPT's I want to fall into the same behavior. Sorry if that wasn't clear. Is there a way to rewrite the slug passed to register_post_type and get the correct template file name? Jan 29, 2018 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.