get_template_part()-wordpress加载一个自定义的模板函数

get_template_part 函数,是wordpress中一个常用的函数。作用是把一个模板文件,加载到另一个模板文件中。作用和php原生的 includerequire 作用类似,但是在wordpress中这个函数更灵活,使用也更方便。

函数原型

# $slug -- 模板的文件名,这个必须有
#    $name -- 名字的扩展部分,这个是可选的
 get_template_part( $slug, $name ) ;

使用实例

# file -- header.php 
get_template_part('seo');  # 如果当前模板文件有seo.php 就包含进来。
get_template_part('seo', 'title'); # 加载的文件是  seo-title.php , 这种方式是很灵活的。

get_template_part 函数,使用更方便。默认就是当前的模板目录,使用页更灵活,可以很方面的加载模板的名字。