$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to require specific PHP files for specific templates|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to require specific PHP files for specific templates

matteradmin8PV0评论

How can I require specific PHP files for specific templates without any hook?

Example: I've one PHP file "autocomplete.php' and once a page template "photographers.php'

Now I want to require 'autocomplete.php' file in functions.php file only for photographers.php template.

How can I do this? I've tried using is_page_template () but didn't success.

Here is code

if(is_page_template('photographers.php')) {   
    require get_template_directory().'/inc/autocomplete.php';
}

Here is screenshot

I've searched before submitted this question in this platform. And i got this post load/require specific php files for specific pages/templates/post types But this post didn't solve my problem. Then I've submitted this question

photographers.php is a custom template. And autocomplete.php is some of custom functions and some of the script. That's all functions and script need to only for photographers.php (Custom Template) so I like to load that function/script only for that custom template

How can I require specific PHP files for specific templates without any hook?

Example: I've one PHP file "autocomplete.php' and once a page template "photographers.php'

Now I want to require 'autocomplete.php' file in functions.php file only for photographers.php template.

How can I do this? I've tried using is_page_template () but didn't success.

Here is code

if(is_page_template('photographers.php')) {   
    require get_template_directory().'/inc/autocomplete.php';
}

Here is screenshot http://prntscr/mchx6a

I've searched before submitted this question in this platform. And i got this post load/require specific php files for specific pages/templates/post types But this post didn't solve my problem. Then I've submitted this question

photographers.php is a custom template. And autocomplete.php is some of custom functions and some of the script. That's all functions and script need to only for photographers.php (Custom Template) so I like to load that function/script only for that custom template

Share Improve this question edited Jan 26, 2019 at 17:02 Md Abul Bashar asked Jan 26, 2019 at 16:11 Md Abul BasharMd Abul Bashar 271 gold badge1 silver badge9 bronze badges 5
  • 2 Why not require it all the time? And why not require it from photographers.php? – kero Commented Jan 26, 2019 at 16:45
  • That should work. Try a relative link. Use require_once rather than require. Consider using dirname(__FILE__) instead of get_template_directory – admcfajn Commented Jan 26, 2019 at 16:50
  • @kero photographers.php is a custom template. and autocomplete.php is some of custom functions and some of script. that's all functions and script need to only for photographers.php (Custom Template) , so why i will load that functions and script in all templates/pages/posts/etc? so I like to load that functions/script only for that custom template. – Md Abul Bashar Commented Jan 26, 2019 at 16:58
  • More ore less PHP functions shouldn't make a difference (depending on wheter you call them or not ;). As for scripts, you should wp_register_script them in your default files and then you can wp_enqueue_script them in the template - no need to include special PHP files – kero Commented Jan 26, 2019 at 17:16
  • @kero I think you didn't understand my question, I want to include some of functions and script, not only script, so If i want to add php function in my theme then did i need to wp_register_script? – Md Abul Bashar Commented Jan 27, 2019 at 0:59
Add a comment  | 

1 Answer 1

Reset to default 0

I had a similar problem, and posted the question here Using Includes in Templates in Document Head .

And I figured out the solution is to use get_template_part(). From my answer (which I checkmarked as correct; the other answer didn't work):

The key is to use the get_template_part in your Template at the beginning (after the template header). The file specified in that function must be in your Theme's root folder, although I think it could also be in the theme's template folder.

get_template_part('my_custom_functions');

This will do the equivalent of a 'include' in a non-WP PHP code. It will 'include' the file called my_custom_functions.php .

See my answer in the link above for more info.

Post a comment

comment list (0)

  1. No comments so far