国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

詳解WordPress開發(fā)中g(shù)et_header()獲取頭部函數(shù)的用法

原創(chuàng) 2017-01-10 13:49:28 275
摘要:這篇文章主要介紹了詳解WordPress開發(fā)中g(shù)et_header()獲取頭部的用法,get_header()函數(shù)在WordPress主題的制作中一定會(huì)用到,需要的朋友可以參考下函數(shù)意義詳解從當(dāng)前主題調(diào)用header.php文件。是不是很簡(jiǎn)單?好吧,如果你是新手的話這里要提醒一下,這里的get和get_children()、get_category中的get略有不同之處。get_header函數(shù)聲

這篇文章主要介紹了詳解WordPress開發(fā)中g(shù)et_header()獲取頭部的用法,get_header()函數(shù)在WordPress主題的制作中一定會(huì)用到,需要的朋友可以參考下

函數(shù)意義詳解
從當(dāng)前主題調(diào)用header.php文件。是不是很簡(jiǎn)單?好吧,如果你是新手的話這里要提醒一下,這里的get和get_children()、get_category中的get略有不同之處。

get_header函數(shù)聲明(定義)
之前寫文章很少會(huì)寫到函數(shù)定義的代碼,后來自己翻看的時(shí)候發(fā)現(xiàn)這個(gè)習(xí)慣不太好,所以決定,只要篇幅允許,就會(huì)把函數(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 ); ?>

很簡(jiǎn)單,從上面的函數(shù)聲明中我們也能看出,該函數(shù)只接受一個(gè)變量作為參數(shù)。

參數(shù)解釋
$name ,從上面的函數(shù)聲明中我們可以看出,$name是一個(gè)字符串型變量,用來調(diào)用header的別名模板,
比如 $name = “ab”;
也就是我們這樣

<?php
  $name = “ab”
  get_header( $name );  
?>

這將會(huì)調(diào)用 header-ab.php 文件作為頭部文件的調(diào)用。

例子:

1.簡(jiǎn)單的 404 頁面

下面的代碼是一個(gè)簡(jiǎn)單模板文件,專門用來顯示 "HTTP 404: Not Found" 錯(cuò)誤的 (這個(gè)文件應(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 準(zhǔn)備的頭部應(yīng)該分別命名為  header-home.php 和 header-404.php 。

 更多關(guān)于WordPress開發(fā)中g(shù)et_header()獲取頭部函數(shù)的用法請(qǐng)關(guān)注PHP中文網(wǎng)(www.miracleart.cn)其他文章! 

發(fā)佈手記

熱門詞條