In-depth analysis of WordPress loaded template Get_template_part function, wordpresstemplate
Recent research official theme Twenty Eleven, there are some things on-line Chinese data is not easy to find, recorded in the blog, is to share, also is a memo, WordPress 3.0 After the beginning of the Get_template_part () this function, It should be a new feature that provides a more diverse selection of article presentation forms.
Examples of twenty eleven are as follows:
Twenty eleven index.php file
Line: 21
<?php if (have_posts ()):?> <?php twentyeleven_content_nav (' Nav-above ');?> <?php/* Start the Loop Rings are used to invoke different types of articles */?> <?php while (Have_posts ()): The_post ();?> <?php get_template_part (' content ', Get_pos T_format ());?> <?php endwhile; >............................<?php endif;?>
Describe:
Load one of the formulated templates into another template (different from containing header,sidebar,footer).
Making it easy for a theme to use sub-templates for code snippet reuse
Used to include the specified template file in the template, only with the specified parameters slug and name can contain the file {slug}-{name}.php, the most important feature is that if the file does not contain a. php file without {name}
How to use:
<?php Get_template_part ($slug, $name)?>
Parameters:
- $slug (mandatory) generic template name
- $name (optional) the specified template name
Example:
Use loop.php in sub-themes
Assuming that the theme folder Wp-content/themes under the parent topic is Twentyten sub-topic twentytenchild, the following code:
<?php get_template_part (' Loop ', ' Index ');?>
The PHP require () function will contain the file according to the following priority level
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 is a bad example, but it's another way of thinking about it)
To add a navigation bar to a theme using a common nav.php file:
<?php get_template_part (' nav '); Navigation Bar (nav.php) ><?php get_template_part (' Nav ', ' 2 '); Navigation Bar #2 (nav-2.php) ><?php get_template_part (' Nav ', ' single '); Navigation Bar to use of single pages (nav-single.php)?>
Get_template_part () 's Hooks
Because in the official theme (twenty eleven) in the Get_template_part () function is a large number of uses, so for the time being, this function should be considered to be a more popular function, before written an article about the function of the specific use of the method, here is also inconvenient to repeat, This paper is mainly aimed at the hook $tag value of the function add_action, because, WP hook in a number of functions in the $tag value is more puzzling.
The difference from ordinary hooks
The $tag of a common hook is a fixed value, and Get_template_part () is a variable value, okay, let's not say, WP does this to give us a simple function of how much trouble, but this setting really brings a lot of convenience to the various theme implementations.
The realization of this principle of the source code as follows, interception from the WordPress source program.
function Get_template_part ($slug, $name = null) {//$tag = "get_template_part_{$slug}"//That is, get_template_part_+ you set at that time $ Slug value 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);}
Instance
Like the above, maybe it's basically a bit of a little out of the way, okay, give me some examples.
Review the usage of Get_template_part ($slug, $name),//If you have such get_template_part in the subject (' Index ', ' photo ');//Then WP will go to the theme root directory index-photo.php File//Then we want to hang a function like the following functions AddFunction ($slug, $name) {echo $slug;} Add_action ("Get_template_part_index", "AddFunction", 10,2);
Get_template_part () function explanation memo
Articles you may be interested in:
- PHP functions for invoking comment templates and looping out comments in WordPress
- A simple way to get the page ID of the template you are using in WordPress
- Introduction of template file for WordPress theme making
- WordPress Theme Authoring in Get header template and bottom template
- Explain the PHP functions used to get Comment templates and search forms in WordPress
http://www.bkjia.com/PHPjc/1091104.html www.bkjia.com true http://www.bkjia.com/PHPjc/1091104.html techarticle in-depth analysis of WordPress loaded template Get_template_part function, wordpresstemplate recently studied the official theme twenty eleven, some things online ready-made Chinese materials are not easy to find, in ...