首页 > 运营 > 帮助中心 > 正文

wordpress官方调用自定义模板get_template_part()函数使用

2020-01-27 17:23:23
字体:
来源:转载
供稿:网友

get_template_part()函数主要用于调用自定义模版,使用get_template_part()函数,可以更灵活的控制主题,get_template_part()函数最大的好处就是大大的减少了代码的重写量。

不过关于代码重写量的问题,其实每个大牛都有各自不同的处理方法,比如原生的php就有require及include两种引入文件方法。

php原生两种方法与Wordpress官方给出get_template_part()函数使用方法都有异曲同工之妙,只是针对wordpress来说,get_template_part()更加的灵活,下面看一下使用说明以及实例,大家能更好的理解。

get_template_part()函数用法

get_template_part()函数的使用很灵活,不仅仅是加载一个模板文件进来,而且还有备用的选项,调用代码如下:

  1. <?php get_template_part( $slug, $name ); ?>

参数:

$slug (必须) 通用的模板名

$name (可选) 指定的模板名

什么意思呢?我们来看一下具体的示例:

  1. <?php get_template_part( 'content', 'blog' ); ?>

意思是说,如果content-blog.php存在,则调用content-blog.php,否则,就调用content.php

至于php的require及include两种方法,这里就不再赘述了。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表