It seems that you need to add styles and scripts to a single post. The best way to achieve this is to make use of the conditional tag is_single()
With that you target specific single posts or all single posts.
Styles should always be loaded in the header. <link>
tags outside the <head></head>
tags are invalid HTML. You should check this recent post on this subject.
On the other hand, scripts can be loaded in the footer of header, though many agree that it should be loaded in the footer.
I would also replace your $priority
of 1
with PHP_INT_MAX
to load your custom styles and scripts after all the other styles and scripts have loaded. This will avoid that your custom scripts and styles will be overwritten by other scripts and styles
Just one note, I don’t know what does urlto
in your code means, but I should accept that you know that this should either be get_template_directory_uri()
or get_stylesheet_directory_uri()
depending on parent theme or child theme and I also accept that you have changed this to make your code simpler for the sake of the question
This is an idea of how your code should look like:
add_action( 'wp_enqueue_scripts',
function ( ) {
if( is_single() ){
wp_enqueue_style( 'products_pages', urlto.'/products_pages.css' );
wp_enqueue_script('tooltipster', urlto.'/js/jquery.tooltipster.min.js', array( 'jquery' ));
}
}
, PHP_INT_MAX, 0 );
References:
EDIT
From the comments to this answer, it seems that your method works. I have one concern though targeting a post through $post
variable. If you specifically need to target justy the single view pages, you are going to run into some issues.
You have to remember, on any page, $post
will hold the value of the last post in the loop. What this means is, if , for instance, the specific post you need to target is the last post on the home page, all changes will affect the home page too, and not just the single page view of the post.
That is why you should make use of is_single()
to target single posts, and not $post
global