詳解WordPress開發(fā)中g(shù)et_header()獲取頭部函數(shù)的用法
函數(shù)意義詳解
從當(dāng)前主題調(diào)用header.php文件。是不是很簡單?好吧,如果你是新手的話這里要提醒一下,這里的get和get_children()、get_category中的get略有不同之處。
get_header函數(shù)聲明(定義)
之前寫文章很少會寫到函數(shù)定義的代碼,后來自己翻看的時候發(fā)現(xiàn)這個習(xí)慣不太好,所以決定,只要篇幅允許,就會把函數(shù)主題貼出來,方便自己翻看。
get_header 函數(shù),聲明(定義)的位置,是在 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函數(shù)的使用
<?php get_header( $name ); ?>
很簡單,從上面的函數(shù)聲明中我們也能看出,該函數(shù)只接受一個變量作為參數(shù)。
參數(shù)解釋
$name ,從上面的函數(shù)聲明中我們可以看出,$name是一個字符串型變量,用來調(diào)用header的別名模板,
比如 $name = “ab”;
也就是我們這樣
<?php $name = “ab” get_header( $name ); ?>
這將會調(diào)用 header-ab.php 文件作為頭部文件的調(diào)用。
例子:
1.簡單的 404 頁面
下面的代碼是一個簡單模板文件,專門用來顯示 "HTTP 404: Not Found" 錯誤的 (這個文件應(yīng)該包含在你的主題中,名為 404.php)
<?php get_header(); ?> <h2>Error 404 - Not Found</h2> <?php get_sidebar(); ?> <?php get_footer(); ?>
2.多種頭部
為不同的頁面顯示不同的頭部
<?php if ( is_home() ) : get_header( 'home' ); elseif ( is_404() ) : get_header( '404' ); else : get_header(); endif; ?>
這些為 home 和 404 準備的頭部應(yīng)該分別命名為 header-home.php 和 header-404.php 。
相關(guān)文章
php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的方法
這篇文章主要介紹了php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的方法,分析了fputcsv()函數(shù)針對csv文件的讀寫操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01