In-depth parsing of the get_template_part function for loading templates in WordPress

Source: Internet
Author: User
This article mainly introduces the get_template_part function for loading templates in WordPress, which focuses on the use of its function hooks. For more information, see Twenty Eleven, there are some items that are not easy to find on the internet. recording them in the blog can be considered as sharing or memo. wordpress 3.0 and later started to have the get_template_part () function, it should be a new function that provides more diverse choices for the document presentation form.

The instance in Twenty Eleven is as follows:

Twenty Eleven index. php file
Row: 21

<? Php if (have_posts ():?> <? Php twentyeleven_content_nav ('Nav-above');?> <? Php/* Start the Loop to call different types of articles in a Loop */?> <? Php while (have_posts (): the_post ();?> <? Php get_template_part ('content', get_post_format ();?> <? Php endwhile;?> ............................ <? Php endif;?>

Description:
Load a specified template to another template (different from header, sidebar, footer ).
Makes it easy to reuse code segments using subtemplates for a topic.

Used to include the specified template file in the template. you only need to specify the slug and name parameters to include the File {slug}-{name }. php, the most important function is to include {name} without this file. php file

Usage:

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

Parameters:

  • $ Slug (required) common Template name
  • $ Name (optional) Template name

Example:

Use loop. php in subtopics

Assume that the parent topic in the wp-content/themes folder is twentyten subtopic twentytenchild. the following code is used:

<?php get_template_part( 'loop', 'index' ); ?>

Php's require () function 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 is a bad example, but it is another way of thinking)
Use the general nav. php file to add a navigation bar to the topic:

<?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) ?>

Get_template_part () hook details
Because the get_template_part () function is widely used in the official theme (Twenty Eleven), this function is a popular function, I have written an article about the specific usage of the function, so it is inconvenient to repeat it here. this article mainly discusses the hook $ tag value in add_action of the function, because, there are always some functions in WP hook that are confusing in the $ tag value.

Differences from common hooks
The $ tag of a common hook is a fixed value, while get_template_part () is indeed a variable value. well, let's not talk about it. how much trouble does wp bring us to implement a simple function, however, this setting does bring a lot of convenience to various theme implementations.
The source code for implementing this principle is as follows, which is truncated from the WordPress source code.

Function get_template_part ($ slug, $ name = null) {// $ tag = "get_template_part _ {$ slug}" // that is, get_template_part _ + 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
As mentioned above, it may be a bit hard to understand.

// Review the usage of get_template_part ($ slug, $ name). // If you use get_template_part ('index', 'Photo ') in the topic '); // then WP will go to the index-photo.php file under the topic Root Directory // then if we want to hang a function, it will be like the following function addFunction ($ slug, $ name) {echo $ slug ;} add_action ("get_template_part_index", "addFunction", 10, 2 );

Detailed description of the get_template_part () function

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.