1

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')?

1

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
    }
}
0

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.

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.