Home > Backend Development > PHP Tutorial > Detailed explanation of the usage of get_header to obtain the header function in WordPress development

Detailed explanation of the usage of get_header to obtain the header function in WordPress development

WBOY
Release: 2016-07-29 09:07:09
Original
962 people have browsed it

Detailed explanation of function meaning
Call the header.php file from the current theme. Is not it simple? Well, if you are a newbie, I would like to remind you that the get here is slightly different from the get in get_children() and get_category.

get_header function declaration (definition)
In the past, when I wrote articles, I rarely wrote code for function definitions. Later, when I looked through it myself, I found that this habit was not very good, so I decided to post the function topic as long as the space allows. Convenient to browse by yourself.
The get_header function is declared (defined) around lines 24 – 36 of the wp=include/general-template.php file.

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');
}
Copy after login

The use of get_header function

<&#63;php get_header( $name ); &#63;>
Copy after login

is very simple. From the function declaration above, we can also see that the function only accepts one variable as a parameter.

Parameter explanation
$name, from the function declaration above, we can see that $name is a string variable used to call the header’s alias template,
For example, $name = “ab”;
That’s what we do

<&#63;php 
  $name = “ab”
  get_header( $name ); 
 
&#63;>
Copy after login

This will call the header-ab.php file as the header file call.

Example:

1. Simple 404 page

The following code is a simple template file specifically used to display "HTTP 404: Not Found" errors (this file should be included in your theme and named 404 .php)

<&#63;php get_header(); &#63;>
<h2>Error 404 - Not Found</h2>
<&#63;php get_sidebar(); &#63;>
<&#63;php get_footer(); &#63;>

Copy after login

2. Multiple headers

Display different headers for different pages

<&#63;php
if ( is_home() ) :
 get_header( 'home' );
elseif ( is_404() ) :
 get_header( '404' );
else :
 get_header();
endif;
&#63;>
Copy after login

These headers prepared for home and 404 should be named header-home.php and header respectively -404.php.

The above has introduced a detailed explanation of the usage of get_header to obtain the header function in WordPress development, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template