• Resolved Designimation

    (@designimation)


    Ok, So I am pretty new to wordpress. I am creating a child theme and want to make a custom page template that enqueues a specific stylesheet when on that custom page template. I created the custom page and was able to select it from the backend and I used this code to enqueue the stylesheet in the functions.php:

    function lander_work_script() {
    if ( is_page_template(‘/page-templates/page-work.php’) ) {
    wp_enqueue_style( ‘lander-work-style’, get_template_directory_uri() . ‘/layouts/page-workStyle.css’);
    }
    }

    add_action(‘wp_enqueue_scripts’, ‘lander-work-script’);

    The problem is the stylesheet is not loading correctly. It is looking for the file in the parent theme from what i can tell. How do I fix this?

Viewing 13 replies - 1 through 13 (of 13 total)
  • When used in a child theme, get_template_directory_uri() refers to the parent theme. In a child theme, you should use get_stylesheet_directory_uri() instead.

    Thread Starter Designimation

    (@designimation)

    OMG !!! haha. You have no idea how much I appreciate this. If I could I would hug you dude (in a not weird way of course).

    I do have a new problem though. I checked my browser and saw that the stylesheet is now enqueuing in successfully, but when I inspect the elements on the custom page template they show they are using a different stylesheet from the parent theme (content-sidebar.css to be precise).

    Is this because of the way I perhaps have my conditional written that targets the custom page template?

    I found that if I add “!important” to the styles of stylesheet I wanted they work. Here is the code of my css file:

    /*
    Theme Name: lander
    Layout: Page Work Style
    */

    @import url(‘../my-simone/layouts/content-sidebar.css’);

    .site-content {
    background-color: black !important;
    }

    I truly appreciate any help, thank you.

    Can you post a link to your site?

    Thread Starter Designimation

    (@designimation)

    It’s a localhost, so I don’t think so.

    What parent theme are you using?

    Thread Starter Designimation

    (@designimation)

    It’s a custom theme built with Underscores. I learned it from lynda.com. It’s called My-Simone

    Is your theme based on this theme: Simone?

    Thread Starter Designimation

    (@designimation)

    yes

    I noticed that you import a stylesheet named content-sidebar.css in your page template’s stylesheet. Would your site break if you didn’t import that stylesheet?

    Thread Starter Designimation

    (@designimation)

    The site doesn’t break but it then defaults to the next stylesheet, which is no-sidebar.css and if you remove that it calls style.css.

    Hmm. What if you did this instead:

    function lander_work_script() {
    if ( is_page_template('page-templates/page-work.php') ) {
    wp_enqueue_style( 'lander-work-style', get_stylesheet_directory_uri() . '/layouts/page-workStyle.css');
    }
    }
    
    add_action('wp_enqueue_scripts', 'lander_work_script', 15, 0);

    The “15” in the add_action() means that this function is executed later than the parent theme’s function, which would mean that your custom page’s stylesheet will be loaded after the parent theme loads its stylesheets, which would mean that the styles in page-workStyle.css would take priority.

    Thread Starter Designimation

    (@designimation)

    YES THAT WORKED !!!!! Dude you saved me from going bald trying to fix this. Thank you so much.

    Thread Starter Designimation

    (@designimation)

    Thanks for the all the help

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Enqueuing Stylesheet for custom page template of child theme.’ is closed to new replies.