Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the get_template_part function of loading template in WordPress

2025-02-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use the get_template_part function that loads templates in WordPress. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The function get_template_part () has been available since wordpress 3.0.It is supposed to be a new function that provides a variety of options for the presentation of articles.

Examples in Twenty Eleven are as follows:

Twenty Eleven index.php file

Line: 21

..

Description:

Load a defined template into another template (unlike containing header,sidebar,footer).

Makes it easy for a topic to use subtemplates for code snippet reuse

To include the specified template file in the template, you can simply use the specified parameters slug and name to include the file {slug}-{name} .php. The most important function is to include the .php file without {name} without this file.

How to use it:

Parameters:

$slug (required) generic template name

$name (optional) specified template name

Example:

Use loop.php in subtopics

Assuming that the parent theme under the theme folder wp-content/themes is the twentyten child theme twentytenchild, then the following code:

The require () function of php will include files at the following priority

1. Wp-content/themes/twentytenchild/loop-index.php

2. Wp-content/themes/twentytenchild/loop.php

3. Wp-content/themes/twentyten/loop-index.php

4. Wp-content/themes/twentyten/loop.php

Navigation (this example sucks, but it's a different way of using it)

Use the generic nav.php file to add a navigation bar to the topic:

Detailed explanation of the hook of get_template_part ()

Because the get_template_part () function is widely used in the official theme (Twenty Eleven), so at present, this function should be regarded as a relatively popular function. I have written an article about the specific use of this function, so it is not convenient to repeat it here. This article mainly discusses the hook $tag value in the add_action of this function, because There are always some functions in WP hook that are more puzzling in the $tag value.

The difference from ordinary hook

The $tag of an ordinary hook is a fixed value, while get_template_part () is indeed a variable value. All right, wp does bring us a lot of trouble to implement a simple function, but this setting does bring a lot of convenience to a variety of theme implementations.

The source code to implement this principle is as follows, intercepted from the WordPress source program.

Function get_template_part ($slug, $name = null) {/ / $tag = "get_template_part_ {$slug}" / / that is, get_template_part_+ the $slug value you set do_action ("get_template_part_ {$slug}", $slug, $name); $templates = array (); if (isset ($name)) $templates [] = "{$slug}-{$name} .php"; $templates [] = "{$slug} .php"; locate_template ($templates, true, false);}

Example

Like what I said above, maybe I don't understand it basically. All right, give me some examples.

/ / review the use of get_template_part ($slug, $name), / / if you do this in the theme get_template_part ('index',' photo'); / / then WP will find the index-photo.php file in the theme root directory / / then we want to hang a function like the following function addFunction ($slug, $name) {echo $slug;} add_action ("get_template_part_index", "addFunction", 10prime2); thank you for reading! This is the end of the article on "how to use the get_template_part function of loading templates in WordPress". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report