Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've multiple header files (header-name.php), they are mainly being used by different custom post types. Is there any way to enqueue script to only a specific header?

At this moment I'm targeting it with conditional tag like if ( 'MYPOSTTYPE' == get_post_type() ) but I'm looking for a better alternative.

do we have something like is_header('name')?

share|improve this question

2 Answers 2

You load the header file probably with get_header(). There is a hook you can use: 'get_header'. You get the called header name as parameter.

add_action( 'get_header', 'wpse_54865_conditional_enqueue' );

function wpse_54865_conditional_enqueue( $name )
{
    if ( 'my_custom_header_name' === $name )
    {
        // register your script loading function
    }
}
share|improve this answer

The wp_enqueue_scripts action is part of the wp_head hook, so the enqueue process isn't really tied to header.php. It's also the appropriate way to enqueue scripts and styles. You could possibly switch to the is_singlular ( 'MYPOSTTYPE' ) conditional tag, but I'm not sure that's really better.

You could set a global variable before the wp_head() function in your header.php's and then test for that variable to decide which scripts to enqueue.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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