首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

WordPress 主題開發:get_header() 與 get_footer() 進階用法

     編輯:PHP 綜合知識

依頁面類型動態載入頁首與頁尾模板

WordPress 提供 get_header() 與 get_footer() 兩個核心函數,支援依條件載入不同模板檔案。

get_header()

get_header( string $name = null )

  • 無參數:載入 header.php
  • 指定 $name:載入 header-{$name}.php
  • 檔案不存在:回退至 wp-includes/theme-compat/header.php

範例:首頁、404 頁使用專屬頁首

if ( is_home() ) {
    get_header('home');
} elseif ( is_404() ) {
    get_header('404');
} else {
    get_header();
}

get_footer()

get_footer( string $name = null )

  • 無參數:載入 footer.php
  • 指定 $name:載入 footer-{$name}.php
  • 檔案不存在:回退至 wp-includes/theme-compat/footer.php

範例:依頁面載入不同頁尾

if ( is_404() ) {
    get_footer('404');
} elseif ( is_home() ) {
    get_footer('home');
} else {
    get_footer();
}

兩函數均定義於 wp-includes/general-template.php,無回傳值。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved