Support » Fixing WordPress » is_page_template in functions.php not working.

  • I have this in functions.php:

    if( is_page_template( 'my-templates/contact.php' ) ) add_action( 'wp_enqueue_scripts', 'contact_script_enqueuer' );

    When I load my Contact page, which uses the contact template, the scripts do not load. If I get rid of the condition, they load fine.

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • i believe is_page_template() can only be called after the template has loaded.

    try

    add_action('wp','mysite_check_template');
    
    function mysite_check_template() {
      ...
    }

    A bit late to the party, but I ran into this issue myself.

    In order to achieve the functionality of ‘is_page_template’, you’ll need to approach the issue a bit differently.

    First, set a variable within functions.php to get the post meta data:

    $template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );

    Then, within your custom function, check if $template_name is equal to the template you want the function to execute on:

    function event_css(){
    
    	if ( $template_name = 'page-event.php' ) {
    		wp_register_style( 'event-css', get_template_directory_uri() . '/pages-event/css/style-event.css' );
    		wp_enqueue_style( 'event-css' );
    	}
    
    }
    
    add_action('wp_enqueue_scripts', 'event_css');
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘is_page_template in functions.php not working.’ is closed to new replies.