Deep analysis of the Get_template_part function _php technique of loading templates in WordPress

Source: Internet
Author: User

Recent research on the official theme Twenty eleven, there are some things online ready-made Chinese information is not easy to find, recorded in the blog, is to share, is also 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 is used in loops to invoke different types of articles/?>
 <?php while (Have_posts ()): The_post ();?> <?php Get_template_part
 (' C Ontent ', Get_post_format ());?>
 <?php endwhile; ...
......... .......
<?php endif;?>

Describe:
Load a set of templates into another template (different from containing header,sidebar,footer).
Makes it easy for a topic to use a child template to implement code snippet reuse

Used to include the specified template file in the template, you can include the file {slug}-{name}.php with the specified parameter slug and name, and the most important feature is that if the file does not contain a. php file without the {name}

How to use:

<?php Get_template_part ($slug, $name)?>

Parameters:

    • $slug (must) generic template name
    • $name (optional) the specified template name

Example:

Using loop.php in a child theme

Suppose the theme folder Wp-content/themes the parent theme is the Twentyten Twentytenchild, then the following code:

<?php get_template_part (' Loop ', ' Index ');?>

PHP's require () function will include files in 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 another way to use 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 in Sing Le pages (nav-single.php)?>

Get_template_part () The hook detailed
because the Get_template_part () function is heavily used in the official theme (twenty eleven), it should be considered a more popular function for the time being, and a previous article has written about the specific use of the function, Here is also inconvenient to repeat, this article mainly aimed at the function of the hook $tag value of the add_action, because, WP hook in a variety of functions in the $tag value is more puzzling.

The difference from a common hook
The ordinary Hook's $tag is a fixed value, and Get_template_part () is a variable value, well, let's not say, WP does this to us to achieve a simple function of how much trouble, but this setting does give a variety of theme implementation brings a lot of convenience.
To achieve 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 the $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 it said, maybe it's basically a little confused, okay? Give the point instance

Review the usage of Get_template_part ($slug, $name),
//If you get_template_part in the subject
(' Index ', ' photo ');
Then WP will go to the subject 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 Detailed memo

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.