创建自定义模板有两种基本方法。他们是
页面选择模板WP file hierarchy请随时检查哪种方法最适合这种情况。
按页面选择模板
此方法用于手动创建页面,并将其添加到菜单中,以便在编辑页面中进行选择。
要添加的步骤
创建模板页并另存为文件名template-{$name}.php
添加页面并从编辑页面菜单右侧边栏上的模板列表中选择此模板,在标题菜单中添加新创建的页面(如果有),例如Tempalte1、Template2,当用户单击Template1链接时,它将加载template-Template1。php,如果步骤2正确完成。类似于Template2链接


<?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();
专业人士
添加新页面时,可以从“编辑页面”菜单中选择页面模板。对于具有相同模板的多个不同页面,每个模板可以具有不同的循环逻辑。对于为一个自定义帖子类型指定的存档页面,请创建一个名为archive-post\\u type\\u name的文件。活动主题文件夹中的php。命名方案为archive-{$posttype}.php
.
因为一个帖子类型只能有一个特定的模板文件。对于不同帖子的切换模板,需要自定义逻辑,如比较帖子slug等,然后加载不同的模板文件。以下说明了该概念
<?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();
专业人士
如果存在post类型,则自动加载一个文件中的统一控制逻辑的完全控制和灵活性仅适用于特定的post类型
