The related PHP functions used to retrieve templates in WordPress use analytic _php techniques

Source: Internet
Author: User

Locate_template () is used to retrieve the highest-priority template files that exist, as well as to load the template files directly.

When a locate_template () function retrieves a child theme, it takes precedence over the template of the child theme and no further retrieves the parent theme.

Usage

Locate_template ($template _names, $load, $require _once);

Parameters

$template _names

(array) (required) the template file name to be introduced (requires an extension) to match the file one by one, and the higher the precedence.

Default value: None

$load

(Boolean) (optional) If set to True, the template file is introduced directly.

Default value: False

$require _once

(Boolean) (optional) If set to True if it has been introduced this time no longer introduced (require_once), it will be introduced (require) whether or not it is introduced.

(This parameter takes effect only if the $load is True).

Default value: False

return value

(string) Returns the path of a specified template file if it exists, otherwise an empty string is returned.

Example

if (locate_template (' content-') $pageName. '. php ')!== {
  //exists, introducing template file
  Get_template_part (' content ', $pageName);
} else{
  //does not exist, direct display content
  the_content ();
}

Other

This function is located at: wp-includes/template.php

Quick Retrieve templates

Get_query_template () is used to quickly retrieve a page template, and it needs to be based on a predetermined page type (type).

It differs from locate_template () in that the type of page needs to be filled out, and the {$type}_template template path filter is generated.
Usage

Get_query_template ($type, $templates);

Parameters

$type

(string) (must) the type of page of the template file to be obtained, and the file name (such as single) that does not have an extension for the template file.

Default value: None

$templates

(array) (optional) a list of alternative templates.

Default value: Empty array

return value

Returns the path to the template file.

Example

If present, introduce a 404-page Template:

if ('!= get_404_template ()) include (Get_404_template ());

Other

This function is located at: wp-includes/template.php

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.