0

I want to show templates of my theme to promotion.So, I need show different templates for only one custom post type.For example, on header menu, if click template 1 button, will show the archive with template1, if click template 2 button, will show archive of the same post type with template2.

I created custom post type archive templates but I cant make them selectable for by visitor.

They will shown same time. How can I do it?

I wrote ?templete=2 to address bar, but it didnt read from wp, so I cant build if-else contidition on this.

3
  • Are you creating pages for different archive page in Page menu and then add in the navigation menu by yourself? May 3, 2020 at 0:19
  • I can do it if it will work, moreover create new loop for archieve query in different page May 3, 2020 at 0:50
  • I have added 2 methods that should probably meet your needs. May 3, 2020 at 4:18

1 Answer 1

0

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

5
  • the problem is how to write condition (if-else) to call different templates.I want to select a template on header menu.for example, click template 1 for example custom post type archieve, or template 2 on header.So, probably, the only way create a query for post type,archieve and template on a blank page May 3, 2020 at 4:35
  • Sounds like 1st method works for you. Say, you create 2 pages, eg Template1, Template2. Add them to menu in navigation. So, you have 2 pages right? Then in Template 1 Edit page, you choose template-template1.php, in Template2 Edit page, you choose template-template2.php. Then you have a menu provide 2 templates for you. If template 1 is loading archive-post-type.template, In template-template1.php, you could require_once( 'archive-post-type.php' ); And you could do different things to template-template2.php. It depends how many templates. You only have to create once. User could select preset. May 3, 2020 at 4:46
  • I have added 1 more step in Method1, is that effect what you want to achieve? May 3, 2020 at 5:39
  • I added some screenshots to help illustrating the example. See if it is what you have expected. May 3, 2020 at 5:52
  • @MuratDeniz What's your exact design or plan or checking strategy? If you could share more formation, I might be able to think of other solutions. May 3, 2020 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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