如何在WordPress 引入自定义 php 文件?

2023/12 29 14:12

在制作模板时有时候会需要引入自定义的php文件,比如自定义的函数,或者将首页分为自定义的部分php文件

1、使用 include 或者 require 函数来引入自定义php文件;

在主题的目录新建test-function.php,您可以在主题目录下的 functions.php 文件中引入,例如;

// 引入自定义PHP文件 
include get_template_directory() . '/test-functions.php';

或者使用require

// 引入自定义PHP文件
require get_template_directory() . '/test-functions.php';

2、如果您要引入的文件是一个页面模块,可以使用自带函数 get_template_part 引入php文件,例如:

// 引入自定义PHP文件
get_template_part('test-functions');

这样可以将index页面分为多个部分,多引入一个topindex.php

<?php get_header();?>
<?php get_template_part('topindex');;?>
<?php get_sidebar(); ?>  
<?php get_footer();?>  

发表回复

欢迎回来 (打开)

(必填)