[Resolved] wp_enqueue_style – how to apply to a single page

Home Forums Support [Resolved] wp_enqueue_style – how to apply to a single page

Home Forums Support wp_enqueue_style – how to apply to a single page

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1194227
    Paul

    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

    #1194240
    Leo
    Staff
    Customer Support

    Hi there,

    is_single is for single posts.

    Maybe try this?
    https://codex.wordpress.org/Conditional_Tags#A_PAGE_Page

    #1194336
    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' );
    	}} );
    #1194814
    Leo
    Staff
    Customer Support

    No problem 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.