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')
?
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.The
wp_enqueue_scripts
action is part of thewp_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 theis_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.