詳解WordPress開發中get_header()擷取頭部函數的用法,wordpressheader
函數意義詳解
從當前主題調用header.php檔案。是不是很簡單?好吧,如果你是新手的話這裡要提醒一下,這裡的get和get_children()、get_category中的get略有不同之處。
get_header函式宣告(定義)
之前寫文章很少會寫到函數定義的代碼,後來自己翻看的時候發現這個習慣不太好,所以決定,只要篇幅允許,就會把函數主題貼出來,方便自己翻看。
get_header 函數,聲明(定義)的位置,是在 wp=include/general-template.php 檔案的第 24 – 36 行左右的位置。
function get_header( $name = null ) { do_action( 'get_header', $name ); $templates = array(); if ( isset($name) ) $templates[] = "header-{$name}.php"; $templates[] = 'header.php'; // Backward compat code will be removed in a future release if ('' == locate_template($templates, true)) load_template( ABSPATH . WPINC . '/theme-compat/header.php');}
get_header函數的使用
<?php get_header( $name ); ?>
很簡單,從上面的函式宣告中我們也能看出,該函數只接受一個變數作為參數。
參數解釋
$name ,從上面的函式宣告中我們可以看出,$name是一個字串型變數,用來調用header的別名模板,
比如 $name = “ab”;
也就是我們這樣
<?php $name = “ab” get_header( $name ); ?>
這將會調用 header-ab.php 檔案作為頭部檔案的調用。
例子:
1.簡單的 404 頁面
下面的代碼是一個簡單模板檔案,專門用來顯示 "HTTP 404: Not Found" 錯誤的 (這個檔案應該包含在你的主題中,名為 404.php)
<?php get_header(); ?>Error 404 - Not Found
<?php get_sidebar(); ?><?php get_footer(); ?>
2.多種頭部
為不同的頁面顯示不同的頭部
<?phpif ( is_home() ) : get_header( 'home' );elseif ( is_404() ) : get_header( '404' );else : get_header();endif;?>
這些為 home 和 404 準備的頭部應該分別命名為 header-home.php 和 header-404.php 。
您可能感興趣的文章:
- WordPress主題製作之模板檔案的引入方法
- WordPress的主題編寫中擷取頭部模板和底部模板
- WordPress主題中添加文章列表頁頁碼導航的PHP代碼執行個體
- WordPress中使主題支援小工具以及添加外掛程式啟用函數
- 實現WordPress主題側邊欄切換功能的PHP指令碼詳解
- 編寫PHP指令碼使WordPress的主題支援Widget側邊欄
- 基於wordpress主題製作的具體實現步驟
- wordpress主題支援自訂菜單及修改css樣式實現方法
http://www.bkjia.com/PHPjc/1089943.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1089943.htmlTechArticle詳解WordPress開發中get_header()擷取頭部函數的用法,wordpressheader 函數意義詳解 從當前主題調用header.php檔案。是不是很簡單?好吧,如果你是...