How can I set multi archive page template on same time for only one custom post type?

There are 2 basic methods to create custom templates. They are

  • Template for Page Selection
  • Individual Template Page according to page type (singular, single, archive and so on) according to WP file hierarchy

Please feel free to examine and see which method best suits the case.

by Template for Page Selection

This method is for creating pages manually and add them to the menu for selection in Edit Page.

Step to add

  1. create a template page and save as filename template-{$name}.php
  2. add a page and select this template from Template list on the right sidebar in the Edit Page menu
  3. appearance > menu, add the newly created pages in the header menu(if any)
    eg. Tempalte1, Template2, when user click on Template1 link, it will load template-template1.php if step2 is done correctly. Similar to Template2 link.

Select template
Add to menu
Template1 test page with template name

<?php
/**
 * The example template for Archive Style1, the following "Archive Style1" will be displayed from the selection menu
 *
 * Template Name: Archive Style1
 *
 * @package myPackage
 */

get_header(); ?>

    <!-- content here -->
    <!-- custom loop -->

<?php
get_footer();

pros

  • could select page template from the edit page menu when adding a new page
  • could apply to many different pages with same template
  • each template could have different loop logic

Individual Template Page

For archive page specified for one custom post type, create a file named archive-post_type_name.php in the active theme folder. The naming scheme is archive-{$posttype}.php.

Because one post type can only have one specific template file. As for witching templates for different posts, custom logic is required such as comparing the post slug and so on and then load different template file. The following illustrate the concept

<?php
get_header(); ?>

    <!-- content here -->
    <!-- custom loop -->
    // may use such as $post->post_name to test and switch

    // if conditon 1
    // file name: content-archive-style1.php
    // get_template_part( 'content, 'archive-style1' );

    // if conditon 2
    // file name: content-archive-style2.php
    // get_template_part( 'content, 'archive-style2' );

<?php
get_footer();

pros

  • automatically load if post type exists
  • unified control in one file
  • full control and flexibility of logic
  • apply only to specific post type

WP File Hierarchy