1

I have one stylesheet linked to my Wordpress page in header section:

<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/style.css" />

Now, I want to load different stylesheets on a few pages or sub-pages.

What's the best/correct way of achieving this?

I'm thinking about writing my own shortcode calling "wp_enqueue_style", is that a good idea?

2

Don't use a shortcode for wp_enqueue_style(); it won't do any good, because by the time the shortcode is parsed, all the relevant hooks ( wp_head, wp_print_styles) will have already fired.

Write a function, in functions.php, to enqueue different style sheets based on is_page( $id ) (where $id can be an ID, slug, or title). Then hook that function into wp_enqueue_scripts.

p.s. note also: you should be using get_stylesheet_uri() in your default stylesheet link, e.g.:

<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_uri(); ?>/style.css" />
0

I you want to just alternate any style href location or so, you can use this filter, example:

add_filter( 'style_loader_src', function($href){
if(strpos($href, "name-of-allowed.css") !== false) {
return $href;
}
return false;
});

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.