《PHP學習:詳解WordPress開發中get_header()獲取頭部函數的用法》要點:
本文介紹了PHP學習:詳解WordPress開發中get_header()獲取頭部函數的用法,希望對您有用。如果有疑問,可以聯系我們。
函數意義詳解
從當前主題調用header.php文件.是不是很簡單?好吧,如果你是新手的話這里要提醒一下,這里的get和get_children()、get_category中的get略有分歧之處.PHP實例
get_header函數聲明(定義)
之前寫文章很少會寫到函數定義的代碼,后來本身翻看的時候發現這個習慣不太好,所以決定,只要篇幅允許,就會把函數主題貼出來,方便本身翻看.
get_header 函數,聲明(定義)的位置,是在 wp=include/general-template.php 文件的第 24 C 36 行左右的位置.PHP實例
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實例
<?php get_header( $name ); ?>
很簡單,從上面的函數聲明中我們也能看出,該函數只接受一個變量作為參數.PHP實例
參數解釋
$name ,從上面的函數聲明中我們可以看出,$name是一個字符串型變量,用來調用header的別名模板,
好比 $name = “ab”;
也就是我們這樣PHP實例
<?php $name = “ab” get_header( $name ); ?>
這將會挪用 header-ab.php 文件作為頭部文件的挪用.PHP實例
例子:PHP實例
1.簡單的 404 頁面PHP實例
下面的代碼是一個簡單模板文件,專門用來顯示 "HTTP 404: Not Found" 錯誤的 (這個文件應該包括在你的主題中,名為 404.php)PHP實例
<?php get_header(); ?> <h2>Error 404 - Not Found</h2> <?php get_sidebar(); ?> <?php get_footer(); ?>
2.多種頭部PHP實例
為分歧的頁面顯示分歧的頭部PHP實例
<?php if ( is_home() ) : get_header( 'home' ); elseif ( is_404() ) : get_header( '404' ); else : get_header(); endif; ?>
這些為 home 和 404 準備的頭部應該分別定名為? header-home.php 和 header-404.php .PHP實例
維易PHP培訓學院每天發布《PHP學習:詳解WordPress開發中get_header()獲取頭部函數的用法》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7765.html