首页 问答 正文

如何自定义WordPress主题的页面模板。

注册会员 / 悠然自学 / 2023-06-11/ 浏览 153 次

自定义WordPress主题的页面模板,需要编写自己的模板文件。

步骤如下:

  1. 在主题文件夹中创建一个新的PHP文件,文件名可以为page-custom.php(“custom”是你的自定义页面模板的名称,可以根据需要更改)。

  2. 在文件中添加以下代码,来指定页面的模板名称、描述和结构。

/*
Template Name: Custom Page
Template Post Type: page
*/

//指定模板的名称和描述

//输出页面的代码结构 
  1. 在模板文件中编写你自己的HTML和PHP代码,根据你的需求来构建页面的布局和内容。

  2. 在WordPress后台编辑页面时,选择“页面模板”选项下的“Custom Page”模板即可应用自己的自定义页面模板。

示例代码:

/*
Template Name: Custom Page
Template Post Type: page
*/

get_header(); ?>

<main id="main" class="site-main" role="main">

  <?php
  // Start the loop.
  while ( have_posts() ) : the_post();

    // Include the page content.
    get_template_part( 'content', 'page' );

    // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) :
      comments_template();
    endif;

  // End the loop.
  endwhile;
  ?>

</main>

<?php get_footer(); 
大家谈论
    我的见解