编辑
2026-04-01
undefined
00

目录

本地开发环境
创建新主题
文章和页面
自定义页面模板
页眉和页脚
模版函数文件
添加主题功能
主页显示
存档页
主题模板文件
主模板
二级模板
分部模板
可变模板
一些常见函数
自定义查询WP_Query
自定义文章类型
自定义字段
自定义查询分页
hook 钩子

本地开发环境

下载并安装Local:https://localwp.com/

打开 Local,点击「+ Create a new site],输入网站名称(例如:test),点击「Continue」->「Continue」,输入用户名、密码、邮箱,点击「Add Site」,稍等片刻,网站就安装完成了。

点击「Open site」,即可打开网站。

可能出现打不开网站的情况。如果之前安装过 VMware Workstation,端口 443 会存在冲突。打开「服务」应用,找到「VMware Workstation Server」,右键「属性],启动类型改为「手动」。

创建新主题

在网站上右键「Go to site folder」,打开网站文件路径,进到test/app/public/wp-content/themes,新建文件夹(例如:mytheme),进入 mytheme/,新建文件 index.phpstyle.css,所有主题都需要这两个文件。

在文件style.css写入:

/* Theme Name: My Theme Author: GAGA Version: 0.1 */

在文件index.php写入:

<?php while(have_posts()){ the_post(); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_content(); ?> <hr> <?php } ?>
  • have_posts() 检查当前页面是否有文章
  • the_post() 显示全部文章
  • the_permalink() 显示永久链接/URL地址
  • the_title() 显示标题
  • the_content() 显示内容
  • the_excerpt() 显示文章摘要
  • get_the_excerpt() 获取文章摘要

可以在主题目录创建一张图片重命名为screenshot.png或者screenshot.jpg,WordPress会自动读取该图片为主题的缩略图。

文章和页面

在主题目录创建文件single.phppage.php,分别是「文章」详情页模版文件和「页面」详情页模版文件。

在文件single.php和page.php写入:

<?php while(have_posts()){ the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_content(); ?> <?php } ?>

自定义页面模板

在主题目录创建文件page-event.php,作为event页面的模版文件。

页眉和页脚

在主题目录创建文件header.phpfooter.php,分别用于显示网站页眉和页脚。

在文件header.php写入:

<!DOCTYPE html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo('charset'); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1"> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <h1>这里是页眉</h1>

在文件footer.php写入:

<p>这里是页脚</p> <?php wp_footer(); ?> </body> </html>
  • wp_head() 头部输出函数,很多插件依赖这个函数
  • wp_footer() 尾部输出函数,很多插件依赖这个函数
  • language_attributes() 显示html标签的语言属性
  • bloginfo() 显示站点信息
  • body_class() 用来给 body 标签添加类

文件index.php修改为:

<?php get_header(); while(have_posts()){ the_post(); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_content(); ?> <hr> <?php } get_footer(); ?>

文件single.php和page.php修改为:

<?php get_header(); while(have_posts()){ the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_content(); ?> <?php } get_footer(); ?>
  • get_header() 加载header.php文件
  • get_footer() 加载footer.php文件

模版函数文件

在主题目录创建文件functions.php,这个是WordPress保留的函数文件,它专门用于添加各种自定义函数代码。

<?php // 定义函数 function my_files() { wp_enqueue_style('my_main_sytles', get_theme_file_uri('/static/css/main.css')); wp_enqueue_script('my_main_js', get_theme_file_uri('/static/js/index.js')); } // 绑定动作,把函数 my_files 挂载到 wp_enqueue_scripts 下面 add_action('wp_enqueue_scripts', 'my_files');
  • wp_enqueue_scripts 钩子用于前端排队脚本和样式,如果是在后台调用,使用 admin_enqueue_scripts 钩子
  • wp_enqueue_style() 用于引入样式文件
  • get_theme_file_uri() 获取主题目录,如果没有使用子主题,使用 get_template_directory_uri() 可获得更好性能
  • wp_enqueue_script() 用于引入js文件

添加主题功能

function my_features() { add_theme_support('title-tag'); register_nav_menu('headerMenu', 'Header Menu'); register_nav_menu('footerMenu', 'Footer Menu'); } add_action('after_setup_theme', 'my_features');
  • add_theme_support() 自动生成页面标题信息(title-tag),如果想通过 hook 调用,则必须使用 after_setup_theme 这个钩子
  • register_nav_menu() 注册一个菜单
  • after_setup_theme 钩子在主题被初始化之后在每个页面加载期间被调用

主页显示

1、设置 -> 阅读 -> 您的主页显示:您的最新文章

模板文件的调用顺序:front-page.php > home.php > index.php

2、设置 -> 阅读 -> 您的主页显示:一个静态页面

页面为“主页”,模板文件的调用顺序:front-page.php > page.php > index.php

页面为“文章页”,模板文件的调用顺序:home.php -> index.php

模板文件index.php能不用就不用。如果打算只设置为“您的最新文章”,可以不用模板文件front-page.php,使用home.php即可;如果打算设置为“一个静态页面”,建议“主页”使用模板文件front-page.php,不要使用模板文件page.php。

存档页

archive.php:默认存档页模版文件。

主题模板文件

主模板

  • index.php
  • home.php
  • single.php
  • page.php
  • archive.php
  • search.php
  • 404.php
  • comment.php

二级模板

  • front-page.php

  • author.php date.php tag.php category.php taxonomy.php

  • attachment.php single-post.php

分部模板

  • 页头 header.php -> get_header();
  • 页脚 footer.php -> get_footer();
  • 侧边栏 sidebar.php -> get_sidebar();
  • 搜索框 searchform.php -> get_search_form();
  • 评论 comments.php -> comments_template();
  • 自定义模板 xxx.php -> get_template_part();

可变模板

  • tag-$id.php tag-1.php tag-2.php

  • tag-$slug.php tag-ios.php tag-android.php

一些常见函数

is_category()判断当前页面是否为分类页面is_author()判断当前页面是否为作者页面is_admin()判断当前页面是否为管理页面is_post_type_archive()判断当前页面是否为文章类型列表页is_main_query()判断当前查询是否为主查询the_author_posts_link()获取当前作者文章归档页面链接the_archive_title()根据查询的对象显示归档标题the_archive_description()返回归档的相关描述the_time()时间函数。the_time('Y-m-d')显示为2022-06-30site_url()返回WordPress安装路径home_url()返回首页地址has_excerpt()通过id判断文章是否设置了摘要paginate_links()获取页面分页链接single_cat_title()用于在分类页输出分类标题get_the_ID()获取当前idget_the_category_list()获取文章所有分类列表get_permalink()获取当前文章的链接,get_permalink(99)获取指定id的文章或页面链接get_pages( )获取已定义页面列表,get_pages(array('child_of' => 2))get_the_content()获取文章内容get_post_type()根据文章id获取文章类型get_post_type_archive_link()获取指定文章类型的文章列表链接get_query_var()查询当前文章的分类及分页wp_get_post_parent_id()获取父级idwp_list_pages()显示页面列表,wp_list_pages(array('title_li' => NULL,'child_of' => 2))wp_nav_menu()引用菜单,wp_nav_menu(array('theme_location' => 'headerMenu'))wp_trim_words()截取字符串,wp_trim_words(get_the_content(), 20)

自定义查询WP_Query

<?php // WP_Query 所使用的参数 $args = array( 'posts_per_page' => 3, //-1一次返回满足查询的所有内容 'post_type' => 'post', 'orderby' => 'title', 'order' => 'ASC', 'meta_query' => array( array( 'key' => '字段名', 'compare' => '>=', 'value' => date('Ymd'), 'type' => 'numeric' ) ) ); // 调用 WP_Query 新建文章查询 $the_query = new WP_Query($args); while ($the_query->have_posts()) { $the_query->the_post(); the_title(); the_excerpt(); } wp_reset_postdata(); //重置WordPress查询 ?>

自定义文章类型

创建一个新的自定义文章类型需要使用 register_post_type 函数来注册,在主题的 functions.php 文件下调用该函数即可。

这里推荐另一种方法,先在wp-content/目录下新建文件夹:mu-plugins,即必须使用(must-use)的插件。进入文件夹,新建文件(例如:my_post_types.php),写入:

<?php function my_post_types() { register_post_type('event', array( 'show_in_rest' => true, //支持古腾堡编辑器 'supports' => array('title', 'editor', 'excerpt'), 'rewrite' => array('slug' => 'events'), 'has_archive' => true, 'public' => true, 'labels' => array( 'name' => 'Events', 'add_new_item' => 'Add New Event', 'edit_item' => 'Edit Events', 'all_items' => 'All Events', 'singular_name' => 'Event' ), 'menu_icon' => 'dashicons-calendar')); } add_action('init', 'my_post_types');

在主题目录创建文件single-event.php,作为自定义文章类型(这里以event举例)详情页模版文件;新建文件archive-event.php,作为自定义文章类型存档页模版文件。

自定义字段

可以考虑使用插件:Advanced Custom Fields,允许添加多种形式的自定义字段类型。

插件安装启用后,可以使用 the_field() 和 get_field() 这两个函数。前者是直接输出字段值,后者是获取字段值以供其他函数调用。

自定义查询分页

一般来说,WordPress分页只能在默认查询中使用,想要自定义查询分页,需要做些工作。

<?php $args = array( 'paged' => get_query_var('paged', 1), 'post_type' => 'post', 'meta_query' => array( array( 'key' => '字段名', 'compare' => '>=', 'value' => date('Ymd'), 'type' => 'numeric' ) ) ); // 调用 WP_Query 新建文章查询 $the_query = new WP_Query($args); while ($the_query->have_posts()) { $the_query->the_post(); the_title(); the_excerpt(); } wp_reset_postdata(); //重置WordPress查询 echo paginate_links(array( 'total' => $the_query->max_num_pages )); ?>

hook 钩子

pre_get_post:修改主查询

function my_adjust_queries() { if (!is_admin() AND is_post_type_archive('post' AND $query->is_main_query())) { $query->set('meta_key', 'event_date'); $query->set('orderby', 'meta_value_num'); $query->set('order', 'ASC'); $query->set('meta_query', array( array( 'key' => 'event_date', 'compare' => '>=', 'value' => date(format('Ymd')), 'type' => 'numeric' ) )); } } add_action('pre_get_post', 'my_adjust_queries');

本文作者:a

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!