主題基礎結構
- 在
wp-content/themes/ 下創建主題文件夾(如 mytheme)。
- 創建
index.php 和 style.css 兩個核心文件。
- 在
style.css 頂部添加主題信息註釋(Theme Name, URI, Description, Author, Version, Tags 等)。
- 添加 300×225 的
screenshot.png 作為預覽圖。
模板拆分與復用
- header.php:包含
<head> 區域、SEO 優化的動態 <title>、樣式表引入、wp_head()、導航菜單(wp_list_categories()、wp_list_pages())。
- footer.php:包含頁腳內容、
wp_footer() 提高插件兼容性。
- sidebar.php:側邊欄結構,配合
dynamic_sidebar() 支持後台小工具拖拽,需在 functions.php 註冊側邊欄(register_sidebar())。
核心模板文件
- index.php:文章列表循環(
have_posts()、the_post()),顯示標題、標簽、時間、評論鏈接、縮略圖、內容(the_content())、分頁導航。
- archive.php:存檔頁面,結構同 index.php,需在 functions.php 添加相關函數。
- single.php:單文章頁,可擴展評論模板(
comments_template())。
- page.php:獨立頁面模板。
常用模板標簽速查
| 函數 | 用途 |
the_permalink() | 文章鏈接 |
the_title() | 文章標題 |
the_tags() | 文章標簽 |
comments_popup_link() | 評論鏈接 |
the_time('Y年n月j日') | 格式化日期 |
the_excerpt() | 文章摘要 |
previous_posts_link() / next_posts_link() | 分頁導航 |
進階功能
- 自定義頁面模板:文件頂部添加
/* Template Name: 自定義模板 */,後台新建頁面時可選。
- 固定鏈接設置:Apache 需開啟
mod_rewrite 並設 AllowOverride All;IIS 需安裝 ISAPI_Rewrite。
- 小工具開發:創建 PHP 文件,在
functions.php 用 register_sidebar_widget() 註冊。
遵循 WordPress 模板層級與規範,即可開發出功能完整、可維護的主題。
- 上一篇文章: PHP TXT 文件讀寫操作實戰指南
- 下一篇文章: PHP Hex 與 RGB 顏色值雙向轉換函數
Copyright ©
程式碼之家 All Rights Reserved