<?php
/**
* Template Name: Custom Template
*/
?>
其中,“Custom Template”是你想要命名的自定义页面模板的名称。
在文件中添加 HTML 和 PHP 代码,来构建自定义页面模板的样式和功能。
将文件保存到 WordPress 主题文件夹的“/wp-content/themes/your-theme-name/”目录中。
在 WordPress 后台页面的“页面”部分,创建一个新页面。
在“页面属性”>“模板”下拉菜单中选择你创建的自定义页面模板的名称,并保存页面。
示例代码:
<?php
/**
* Template Name: Custom Template
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the Loop.
while ( have_posts() ) :
the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
endwhile; // End the loop.
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
?>
此代码创建了一个名为“Custom Template”的自定义页面模板,然后加载了一个顶部导航栏、主体区域、边栏和底部区域。其中,在 while 循环中的“template-parts/content-page.php”文件会加载所选页面的内容。