WordPress 主题是构建网站外观和功能的关键。在这个教程中,我们将学习如何创建和管理 WordPress 主题。
基础概念
- 主题文件:主题通常包含多个文件,如
style.css
、functions.php
等。 - 模板文件:模板文件定义了网页的结构,例如
header.php
、footer.php
等。
创建第一个主题
- 创建主题文件夹:在 WordPress 安装目录下创建一个新文件夹,命名为
my-first-theme
。 - 添加
style.css
文件:在my-first-theme
文件夹中创建一个名为style.css
的文件,并添加以下内容:
/*
Theme Name: My First Theme
Description: My first custom WordPress theme
Author: Your Name
*/
- 添加
functions.php
文件:在my-first-theme
文件夹中创建一个名为functions.php
的文件,并添加以下内容:
<?php
function my_first_theme_enqueue_scripts() {
wp_enqueue_style( 'my-first-theme-style', get_stylesheet_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'my_first_theme_enqueue_scripts' );
- 激活主题:在 WordPress 管理后台的“外观”>“主题”页面中,激活
my-first-theme
主题。
主题定制
- 修改样式:在
style.css
文件中修改 CSS 代码,以改变主题的外观。 - 添加功能:在
functions.php
文件中添加 PHP 代码,以增加主题的功能。
资源
WordPress 主题示例