Site logo

Archived topic

wp_enqueue_style - how to apply to a single page

3 replies · Started by Paul on March 13, 2020

Viewing posts 1–4 of 4

This code works well to link in the stylesheet style_test.css

/* add link to stylesheet to the head section */
add_action( 'wp_enqueue_scripts', function() {
		$child_folder = trailingslashit( get_stylesheet_directory_uri() );
		wp_enqueue_style( 'style', $child_folder . 'style_test.css' );
	} );

I want to only load it into the <head> for one page and have tried this:

/* add link to stylesheet to the head section of page Test (id=337)
add_action( 'wp_enqueue_scripts', function() {
		if(is_single(337)) {
		$child_folder = trailingslashit( get_stylesheet_directory_uri() );
		wp_enqueue_style( 'style', $child_folder . 'style_test.css' );
	}} );
	*/

But this stops it working altogether.
Thank you, Paul

Perfect - thank you
This code in functions.php works as epxected

add_action( 'wp_enqueue_scripts', function() {
		if(is_page('337')) {
		$child_folder = trailingslashit( get_stylesheet_directory_uri() );
		wp_enqueue_style( 'style', $child_folder . 'style_test.css' );
	}} );

No problem :)

This archived topic is closed to new replies.