Home > Backend Development > PHP Tutorial > In-depth analysis of the get_template_part function of loading templates in WordPress, wordpresstemplate_PHP tutorial

In-depth analysis of the get_template_part function of loading templates in WordPress, wordpresstemplate_PHP tutorial

WBOY
Release: 2016-07-12 09:00:54
Original
816 people have browsed it

In-depth analysis of the get_template_part function of loading templates in WordPress, wordpresstemplate

Recently researching the official theme Twenty Eleven, there are some things that are difficult to find on the Internet in Chinese, so I will record them in the blog , it can be regarded as a sharing or a memo. The get_template_part() function has been available since WordPress 3.0. It should be a new function to provide more diverse choices for article presentation forms.

The examples in Twenty Eleven are as follows:

Twenty Eleven index.php file
Row: 21

<&#63;php if ( have_posts() ) : &#63;>
 <&#63;php twentyeleven_content_nav( 'nav-above' ); &#63;>
 <&#63;php /* Start the Loop 在循环中使用以调用不同类型的文章 */ &#63;>
 <&#63;php while ( have_posts() ) : the_post(); &#63;>
 <&#63;php get_template_part( 'content', get_post_format() ); &#63;>
 <&#63;php endwhile; &#63;>
............................
<&#63;php endif; &#63;>
Copy after login

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:

<&#63;php get_template_part( $slug, $name ) &#63;>
Copy after login

Parameters:

  • $slug (required) Universal template name
  • $name (optional) The specified template name

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:

<&#63;php get_template_part( 'loop', 'index' ); &#63;>
Copy after login

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:

<&#63;php get_template_part( 'nav' );      // Navigation bar (nav.php) &#63;>
<&#63;php get_template_part( 'nav', '2' );   // Navigation bar #2 (nav-2.php) &#63;>
<&#63;php get_template_part( 'nav', 'single' ); // Navigation bar to use in single pages (nav-single.php) &#63;>

Copy after login

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);
}

Copy after login

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);
Copy after login

get_template_part() function detailed explanation memo

Articles you may be interested in:

  • Detailed explanation of the PHP function that calls comment templates and loops to output comments in WordPress
  • How to get the page ID of the template used in WordPress Method
  • How to introduce template files in WordPress theme production
  • Get the head template and bottom template in WordPress theme writing
  • Explain how to get comment templates and search in WordPress PHP functions for forms

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1091104.htmlTechArticleIn-depth analysis of the get_template_part function of loading templates in WordPress. WordPresstemplate recently studied the official theme Twenty Eleven, and there are some things readily available online. It’s hard to find Chinese information, at...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template