Home > Backend Development > PHP Tutorial > Detailed explanation of the usage of get_header() to obtain the header function in WordPress development, wordpressheader_PHP tutorial

Detailed explanation of the usage of get_header() to obtain the header function in WordPress development, wordpressheader_PHP tutorial

WBOY
Release: 2016-07-12 09:01:09
Original
883 people have browsed it

Detailed explanation of the usage of get_header() to obtain the header function in WordPress development, wordpressheader

Detailed explanation of the meaning of the function
Call the header.php file from the current theme. Isn't it very 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 the code of function definitions. Later, when I read it, I found that this habit was not very good, so I decided that as long as the space allowed, I would post the function topic to make it easier for me to read it.
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

Usage of get_header function

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

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

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

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

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

Example:

1. Simple 404 page

The following code is a simple template file specifically designed 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. Various heads

Show 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 for home and 404 should be named header-home.php and header-404.php respectively.

Articles you may be interested in:

  • How to introduce template files in WordPress theme production
  • Get the header template and bottom template in WordPress theme writing
  • Example of PHP code for adding page number navigation to article list page in WordPress theme
  • Enable theme to support widgets and add plug-in enabling function in WordPress
  • PHP script to implement WordPress theme sidebar switching function Detailed explanation
  • Write PHP script to make WordPress theme support Widget sidebar
  • Specific implementation steps based on wordpress theme production
  • Wordpress theme supports custom menu and modified css style implementation Method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1089943.htmlTechArticleDetailed explanation of the usage of get_header() in WordPress development to obtain the header function, detailed explanation of the meaning of wordpressheader function to call header from the current theme. php file. Isn't it very simple? Well, if you are...
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