0

I'm using a 'specialized' page template called 'page-courses.php', following the codex: http://codex.wordpress.org/Page_Templates#Specialized_Page_Template

In my child theme's functions.php I want to add a php include only when using the page template.

I've used get_page_template() as a way of identifying the page template

if(strtolower(basename(get_page_template()))=='page-courses.php'){
   include_once('functions_courses.php');
}

However, get_page_template() doesn't return anything at that point in the execution. If I run it after the loop...

function is_courses_page(){
  if(strtolower(basename(get_page_template()))=='page-courses.php'){
     include_once('functions_courses.php');
  }
}
add_action('loop_end', 'is_courses_page');

... then 'page-courses.php' is returned, but too late for including my functions_courses.php file which includes enqueues. I'm not sure which action reference to use

I'm quite new to all this, so maybe I'm going about it in completely the wrong way, so any guidance would be much appreciated.

  • if you are using the template as described in the linked Codex chapter, try to use is_page('courses') in your conditional. – Michael Jun 2 '14 at 9:59
  • Why can't you hard code the PHP into the template or conditionally execute the function using the is_page_template() conditional tag? – Brad Dalton Jun 2 '14 at 10:32
  • Thanks for the replies. @Michael that didn't return anything for me. My custom post type is indeed 'courses' though. – Chris Jun 2 '14 at 11:53
  • @BradDalton that is a good suggestion and it works :) I guess I've been so used to using functions.php I never considered adding the include to the template itself. – Chris Jun 2 '14 at 11:54
  • Ah. Spoke too soon, @BradDalton. Your solution worked well for the enqueues, but there are functions/hooks (plugin related) that need fire before the template is loaded. – Chris Jun 2 '14 at 12:07
2

Wordpress had a built-in conditional tag for that:

if ( is_page_template( 'page-courses.php' ) ) {
     include_once('functions_courses.php');
};

More infos on the codex

| improve this answer | |
  • 1
    Thanks. But for some reason it's returning false. – Chris Jun 2 '14 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.