I recently researched the official theme of Twenty Eleven. There are some things that are difficult to find in Chinese on the Internet. I recorded them in my blog as a sharing and as a memo. The get_template_part() function has been available since WordPress 3.0. It should be a new function that provides more diverse choices for article presentation forms.
The examples in Twenty Eleven are as follows:
Twenty Eleven index.php file
Row: 21
<?php if ( have_posts() ) : ?> <?php twentyeleven_content_nav( 'nav-above' ); ?> <?php /* Start the Loop 在循环中使用以调用不同类型的文章 */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> ............................ <?php endif; ?>
Description:
Load a specified template into another template (different from including header, sidebar, footer).
Makes it easy for a theme to use subtemplates to reuse code snippets
is used to include the specified template file in the template. You only need to specify the parameters slug and name to include the file {slug}-{name}.php. The most important function is that if there is no such file, there will be no {name }.php file
Usage:
<?php get_template_part( $slug, $name ) ?>
Parameters:
Example:
Use loop.php in child theme
Assume that the parent theme under the theme folder wp-content/themes is twentyten child theme twentytenchild, then the following code:
<?php get_template_part( 'loop', 'index' ); ?>
php’s require() function will include files according to the following priorities
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)
Use the common nav.php file to add a navigation bar to the theme:
<?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 single pages (nav-single.php) ?>
Detailed explanation of get_template_part() hook
Because the get_template_part() function is widely used in the official theme (Twenty Eleven), so for now, this function should be considered a relatively popular function. I have written an article before about the specific use of this function. It is not convenient to go into details 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 whose $tag value is confusing.
Differences from ordinary hooks
The $tag of an ordinary hook is a fixed value, but get_template_part() is indeed a variable value. Let’s not talk about how much trouble wp does to implement a simple function, but this setting does bring diversified functions. Theme implementation brings a lot of convenience.
The source code to implement this principle is as follows, taken from the WordPress source program.
function get_template_part( $slug, $name = null ) { //$tag = "get_template_part_{$slug}" //也就是,get_template_part_+你当时设置的$slug值 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
As stated above, I may not understand it at all. Please give me some examples
//复习一下get_template_part($slug, $name)的用法, //如果你在主题里这样 get_template_part( 'index' , 'photo'); //那么 WP 会去找主题根目录下 index-photo.php 文件 //那么我们想挂一个函数的话就得像如下 function addFunction ($slug, $name){ echo $slug; } add_action("get_template_part_index","addFunction",10,2);
get_template_part() function detailed explanation memo