How to get the header function using get_header in WordPress development

Source: Internet
Author: User
: This article describes how to use get_header to obtain the header function in WordPress development. For more information about PHP tutorials, see. Function meanings
Call the header. php file from the current topic. Is it easy? Well, if you are a newbie, please note that the get here is slightly different from the get in get_children () and get_category.

Get_header function declaration (definition)
I wrote a few articles in my previous articles and wrote code for function definition. later I found that this habit was not very good. so I decided that as long as the length permits, I would post the function topic so that I could easily look at it myself.
The location of the get_header function, declared (defined), is around 24th-36 rows in 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');}

Use of the get_header function

<?php get_header( $name ); ?>

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

Parameter description
$ Name, as shown in the preceding function declaration, $ name is a string variable used to call the alias template of the header,
For example, $ name = "AB ";
That's what we do.

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

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 used to display "HTTP 404: Not Found" errors (this file should be included in your topic, named 404.php)

<?php get_header(); ?>Error 404 - Not Found<?php get_sidebar(); ?><?php get_footer(); ?>

2. multiple headers

Display different headers for different pages

<?phpif ( is_home() ) : get_header( 'home' );elseif ( is_404() ) : get_header( '404' );else : get_header();endif;?>

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

The above describes how to get the header function using get_header in WordPress development, including some content, and hope to help friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.