TRUYỀN MỘT BIẾN CHO GET_TEMPLATE_PART

      22
get_template_part( string$slug, string$name=null, array$args=array())

Loads a template part into a template.

Bạn đang xem: Truyền một biến cho get_template_part


Description #Description

Provides a simple mechanism for child themes to lớn overload reusable sections of code in the theme.

Includes the named template part for a theme or if a name is specified then a specialised part will be included. If the theme contains no slug.php tệp tin then no template will be included.

The template is included using require, not require_once, so you may include the same template part multiple times.

For the $name parameter, if the file is called "slug-special.php" then specify "special".

Top ↑

Parameters #Parameters

$slug

(string)(Required)The slug name for the generic template.

$name

(string)(Optional)The name of the specialised template.

Default value: null

$args

(array)(Optional) Additional arguments passed to the template.

Default value: array()

Top ↑

Return #Return

(void|false) Void on success, false if the template does not exist.

Top ↑

More Information #More Information

Usage #Usage

get_template_part( $slug );get_template_part( $slug, $name );Note: get_template_part() fails silently

Top ↑

Source #Source

File: wp-includes/general-template.php


function get_template_part( $slug, $name = null, $args = array() ) /** * Fires before the specified template part tệp tin is loaded. * * The dynamic portion of the hook name, `$slug`, refers khổng lồ the slug name * for the generic template part. * *
since 3.0.0 *
since 5.5.0 The `$args` parameter was added. * *
param string $slug The slug name for the generic template. *
param string
Expand full source codeCollapse full source codeView on Trac

Top ↑

Related #Related

Top ↑

Uses #Uses

UsesUsesDescription
wp-includes/general-template.php:get_template_part

Fires before a template part is loaded.

wp-includes/general-template.php:get_template_part_$slug

Fires before the specified template part file is loaded.

wp-includes/plugin.php:do_action()

Calls the callbaông chồng functions that have been added to lớn an action hook.

Xem thêm: Hướng Dẫn Cách Làm Google Biểu Mẫu ) Trong Google Drive, Cách Thêm Google Form Vào Website Wordpress

wp-includes/template.php:locate_template()

Retrieve the name of the highest priority template file that exists.

Top ↑

Changelog #Changelog

ChangelogVersionDescription
5.5.0The $args parameter was added.
3.0.0Introduced.

Top ↑

User Contributed Notes #User Contributed Notes


Skip to note 3 content
You must log in to vote on the helpfulness of this noteVote results for this note: 34You must log in lớn vote on the helpfulness of this note
*
Contributed by pcarvalho—4 years ago

the old codex had this entry about “Passing Variables to Template”

Because the template is being required, it will not have sầu access to any variables you define within the calling theme’s PHP code, unless you explicitly declare them as global.

However, load_template(), which is called indirectly by get_template_part() extracts all of the WP_Query query variables, inlớn the scope of the loaded template. So you can use set_query_var() to lớn make your variable available lớn the template part.

// You wish lớn make $my_var available to lớn the template part at `content-part.php`set_query_var( "my_var", $my_var );get_template_part( "content", "part" );
FeedbackLog in to add feedbackSkip to note 9 content
You must log in khổng lồ vote on the helpfulness of this noteVote results for this note: 2You must log in to lớn vote on the helpfulness of this note

*
Contributed by Andy Macaulay-Brook—5 months ago
Sometime since January 2019, the function has changed khổng lồ return false if no template tệp tin is found. This isn’t noted in the code comments or changelog. The note that the function fails silently is no longer correct.

This now makes it easy for plugins to lớn provide a template fallbachồng (or allow a template override, depending on your perspective) lượt thích this:

if ( false === get_template_part( $slug, $name, $args ) // mặc định output
FeedbackLog in to add feedbackSkip lớn note 11 content
You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to lớn vote on the helpfulness of this note

*
Contributed by Ahmad Karim—11 months ago
You can also access the template variables in your template part file by doing this:

extract( $args );emang đến $variable1;echo $variable2;Your get_template_part will look something lượt thích this:

$args = array("variable1" => "$variable1 Value","variable2" => "$variable2 Value",);get_template_part( "template-parts/template-part-file", null, $args ); – Keep in mind that the third argument $args has been added since version 5.5


FeedbackLog in khổng lồ add feedbackSkip khổng lồ note 12 content
You must log in to vote on the helpfulness of this noteVote results for this note: -1You must log in to vote on the helpfulness of this note
*
Contributed by luboslives—1 year ago

If your web VPS is using ModSecurity, it will likely bloông xã attempts khổng lồ inline SVGs using a PHP.. method like file_get_contents(). You can use get_template_part() lớn inline SVGs.

Create a duplicate of your SVG, where icon.svg becomes inline-ibé.svg.phpSkip lớn note 13 content
You must log in lớn vote on the helpfulness of this noteVote results for this note: -2You must log in lớn vote on the helpfulness of this note
*
Contributed by Felipe Marcos—2 years ago

Passing variables lớn get_template_part()

It’s not possible khổng lồ pass variables to a template loaded via get_template_part(). I came up with a workaround that doesn’t rely on global variables for it lớn work.

/* functions.php */ function get_custom_template($tệp tin = "", $arguments = <>) extract($arguments); include( locate_template( $file .".php", false, false ) );/* loop.php */ while( have_posts() ): the_post(); get_custom_template( "content", <"is_featured" => true >);endwhile;Now, when we try to lớn use $is_featured, it will be available lớn nội dung.php only.

/* nội dung.php */ the_title(); the_content(); var_dump($is_featured); // true
FeedbackLog in to add feedback

You must log in before being able lớn contribute a note or feedback.