WordPress主题开发指南
wordpress主题开发文档
- 主题结构
标准的WordPress主题包含以下核心文件和目录:
- style.css:主题样式表,必须包含主题元数据
- index.php:默认模板文件
- functions.php:主题功能配置文件
- screenshot.png:主题预览图片(880x660像素)
- header.php:页眉模板
- footer.php:页脚模板
- sidebar.php:侧边栏模板
- single.php:单篇文章模板
- page.php:页面模板
- archive.php:归档页面模板
- search.php:搜索结果模板
- 404.php:404错误页面模板
- front-page.php:首页模板
- home.php:博客首页模板
- category.php:分类模板
- tag.php:标签模板
- author.php:作者模板
- date.php:日期归档模板
- taxonomy.php:自定义分类法模板
- attachment.php:附件模板
- image.php:图片附件模板
- 主题元数据
在style.css文件中必须包含以下信息:
/
Theme Name: My Theme
Theme URI: http://example.com
Author: John Doe
Author URI: http://example.com
Description: A description of the theme
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: my-theme
/
- 模板标签
常用模板标签包括:
- <?php bloginfo('name'); ?>:网站名称
- <?php bloginfo('url'); ?>:网站URL
- <?php bloginfo('stylesheet_url'); ?>:主题样式表路径
- <?php bloginfo('template_url'); ?>:主题目录路径
- <?php bloginfo('description'); ?>:网站描述
- <?php bloginfo('version'); ?>:WordPress版本
- <?php bloginfo('html_type'); ?>:HTML类型
- <?php bloginfo('charset'); ?>:字符集
- <?php bloginfo('pingback_url'); ?>:Pingback URL
- <?php bloginfo('stylesheet_directory'); ?>:样式表目录路径
- 查询标签
常用查询标签包括:
- <?php if ( have_posts() ) : ?>:检查是否有文章
- <?php while ( have_posts() ) : the_post(); ?>:循环文章
- <?php the_title(); ?>:文章标题
- <?php the_content(); ?>:文章内容
- <?php the_excerpt(); ?>:文章摘要
- <?php the_permalink(); ?>:文章链接
- <?php the_time('F j, Y'); ?>:文章日期
- <?php the_author(); ?>:作者名称
- <?php the_category(', '); ?>:文章分类
- <?php the_tags('Tags: ', ', ', ''); ?>:文章标签
- <?php previous_posts_link('« Previous'); ?>:上一页链接
- <?php next_posts_link('Next »'); ?>:下一页链接
- 导航菜单
使用wp_nav_menu函数创建导航菜单:
<?php wp_nav_menu( array(
'theme_location' => 'primary',
'menu_class' => 'nav-menu'
) ); ?>
需要在functions.php中注册菜单:
function register_my_menus() {
register_nav_menus(
array(
'primary' => ( 'Primary Menu' ),
'footer' => ( 'Footer Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
- 小工具区域
在functions.php中注册小工具区域:
function my_theme_widgets_init() {
register_sidebar( array(
'name' => __( 'Sidebar', 'my-theme' ),
'id' => 'sidebar-1',
'before_widget' => '',
'before_title' => '
',
'after_title' => '
',) );
}
add_action( 'widgets_init', 'my_theme_widgets_init' );
在模板文件中显示小工具:
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<?php endif; ?>
- 脚本和样式
在functions.php中注册脚本和样式:
function my_theme_scripts() {
wp_enqueue_style( 'my-style', get_stylesheet_uri() );
wp_enqueue_script( 'my-script', get_template_directory_uri() . '/js/script.js', array('jquery'), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
- 自定义头像
在functions.php中添加支持:
add_theme_support( 'custom-header', array(
'default-text-color' => '000000',
'width' => 960,
'height' => 130,
'header-text' => false
) );
- 自定义背景
在functions.php中添加支持:
add_theme_support( 'custom-background', array(
'default-color' => 'e8e8e8'
) );
- 图片尺寸
在functions.php中注册图片尺寸:
function my_theme_setup() {
add_image_size( 'custom-size', 300, 200, true );
}
add_action( 'after_setup_theme', 'my_theme_setup' );
- 页面模板
创建自定义页面模板:
<?php
/
Template Name: Full Width Page
/
get_header();
?>
<?php get_footer(); ?>
- 模板继承
使用get_template_part函数加载模板部分:
<?php get_template_part( 'template-parts/content', 'page' ); ?>
- 主题设置
使用theme_mods存储主题设置:
function my_theme_customize_register( $wp_customize ) {
$wp_customize->add_setting( 'my_theme_color', array(
'default' => '#ffffff',
'sanitize_callback' => 'sanitize_hex_color',
) );
$wp_customize->add_control( new WP_Customize_Color_Control(
$wp_customize,
'my_theme_color',
array(
'label' => __( 'Background Color', 'my-theme' ),
'section' => 'colors',
'settings' => 'my_theme_color'
)
) );
}
add_action( 'customize_register', 'my_theme_customize_register' );
- 邮件发送
使用wp_mail函数发送邮件:
wp_mail( '[email protected]', 'Subject', 'Message' );
-
缓存处理
使用WP Super Cache或W3 Total Cache等插件优化性能。 -
国际化
使用__()和_e()函数实现多语言支持:
<?php _e( 'Hello, World!', 'my-theme' ); ?>
<?php echo __( 'Hello, World!', 'my-theme' ); ?>
-
错误处理
使用is_404(), is_search(), is_home()等函数判断当前页面类型。 -
多站点支持
使用is_multisite()函数检查是否为多站点环境。 -
安全性
使用esc_attr(), esc_html(), esc_url()等函数转义输出内容。 -
性能优化
使用wp_cache_get()和wp_cache_set()进行缓存处理。







