最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp enqueue style - Set different css stylesheet for specific pages

programmeradmin1浏览0评论
This question already has answers here: How do I load custom scripts and styles for a page? (2 answers) Closed 5 years ago.

Is it possible to assign a css stylesheet to specific pages only?

If so how?

Can I use something like this? Not sure how to specify the pages though?

add_action( 'wp_enqueue_scripts', 'enqueue_theme_css' );

function enqueue_theme_css()
{
    wp_enqueue_style(
        'default',
        get_template_directory_uri() . '/css/default.css'
    );
}
This question already has answers here: How do I load custom scripts and styles for a page? (2 answers) Closed 5 years ago.

Is it possible to assign a css stylesheet to specific pages only?

If so how?

Can I use something like this? Not sure how to specify the pages though?

add_action( 'wp_enqueue_scripts', 'enqueue_theme_css' );

function enqueue_theme_css()
{
    wp_enqueue_style(
        'default',
        get_template_directory_uri() . '/css/default.css'
    );
}
Share Improve this question edited May 31, 2019 at 14:44 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked May 31, 2019 at 14:42 Gavin ReynoldsonGavin Reynoldson 1054 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, it is possible. All you need to do is to check whether you are on a specific page or not. For example, this will enqueue your scripts only for the page that has the contact-us slug:

add_action( 'wp_enqueue_scripts', 'enqueue_theme_css' );

function enqueue_theme_css()
{
    if( is_page( 'contact-us' ) ){
        wp_enqueue_style(
            'default',
            get_template_directory_uri() . '/css/default.css'
        );
    }
}

You can take a look into the is_page() function for more informations.

发布评论

评论列表(0)

  1. 暂无评论