0

I want to import a style only on specific site types (articles (single.php), categories (category.php) and 404 page (404.php)). I tried to use is_page() but I don't know if it works for different site types. Thank you for your help.

Update this is what I tried

wp_register_style('style-narrow', '/wp-content/themes/my theme/css/style-article-narrow.css');
    if ( is_singular() ) {
        wp_enqueue_style('style-narrow', 'general', '1.0', 'screen');
    }

and

wp_register_style('style-narrow', '/wp-content/themes/my theme/css/style-article-narrow.css');
        if ( is_page_template( 'category.php' ) ) {
            wp_enqueue_style('style-narrow', 'general', '1.0', 'screen');
        }
  • Please edit your question and paste in the relevant code, to show us what you've tried so far. This will make it easier for us to provide useful answers. – Pat J Dec 1 '17 at 19:35
  • you can call wp_enqueue_style in the template file. – mmm Dec 1 '17 at 19:35
  • It is working, but only before wp_head() is called, so I have to create a new header – p_0g_amm3_ Dec 1 '17 at 20:21
1

is_tax() and is_404() perhaps?

https://codex.wordpress.org/Conditional_Tags


UPDATE:

I'm sorry my original answer was vague, to elaborate better:

You can add conditional tags in a wp_enqueue_scripts hook, choosing to register and enqueue when criteria is met, like so:

add_action( 'wp_enqueue_scripts', 'mythemes_scripts' );
function mythemes_scripts() {

    if (is_category()) {
        wp_register_style(
            'categoryStyles',  
            get_template_directory_uri() . '/style-category.css', 
            array(), // maybe the primary style.css handle here 
            filemtime( get_template_directory() . '/style-category.css' )
        );
        wp_enqueue_style( 'categoryStyles');
    }

    if (is_404()) {
        wp_register_style(
            'fourohfourStyles',  
            get_template_directory_uri() . '/style-404.css', 
            array(), // maybe the primary style.css handle here 
            filemtime( get_template_directory() . '/style-404.css' )
        );
        wp_enqueue_style( 'fourohfourStyles');
    }

}

The codex link above has many more conditional tags for situations like is_archive().

I've put "// maybe the primary style.css handle here" as doing so makes the main style sheet load before your conditional style sheets - so any CSS redefinitions are overwriting properly.

The filemtime() for version numbering (instead of '1.0') will help with cache-breaking, which is valuable while trying to troubleshoot with multiple CSS includes that are presumably redefining.

And if this code is for a plugin instead of a theme you can swap get_template_directory_uri()/get_template_directory_uri() out for plugins_url()/plugin_dir_path() respectively.

|improve this answer|||||
  • This is probably correct, but as it stands it reads more like a comment. Please expand it (perhaps with a code sample) to be more helpful. See How do I write a good answer. – Pat J Dec 1 '17 at 19:53
  • Not working for me (maybe I'm doing something wrong..) code: if ( is_404() ) { wp_enqueue_style('style-narrow', 'general', '1.0', 'screen'); } – p_0g_amm3_ Dec 1 '17 at 20:20
  • sorry, I've updated my post. With the above I was able to get individual .css files to load for Category pages, 404 pages, etc. – David Sword Dec 1 '17 at 21:27
0

As mmm pointed out, I could also use wp_enqueue_style in my template file, so I created another header (header_single.php) with

<?php wp_enqueue_style('style-narrow', 'general', '1.0', 'screen'); ?>
<?php wp_head(); ?>

Note that the style has to be enqueued before wp_head() is called.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.