WordPress函数:get_template_part 用法简介

追格官方小助手/ 2023年03月05日/ WordPress/ 浏览 973

WordPress 函数 get_template_part 和 require 差不多,但是比 require 多两个优点。


一、对子主题的支持。


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


上述代码,会依次寻找下面的文件。


wp-content/themes/twentytenchild/loop-index.php

wp-content/themes/twentyten/loop-index.php

wp-content/themes/twentytenchild/loop.php

wp-content/themes/twentyten/loop.php


二、可以给文件传入参数。


get_template_part(
	'loop',
	'index',
	array(
		'key'	=> 'value',
		'key2'	=> 'value2'
	)
);


在 loop-index.php 中可以使用下面的方式读取参数。


var_dump( $args );	// Everything
echo $args['key'];	// Specific values


发表评论

暂无评论,抢个沙发...

客服 工单