OK, that was helpful. But I need it a bit more complicated. I need a script to add a custom css to all pages, exept on one. How can I do that?
1 Answer
add this to end of functions.php
function mmk_enqueue_script(){
if( !is_page(123) ){
wp_enqueue_style('responsive-css', get_stylesheet_directory_uri() . '/custom.css', false);
}
}
add_action( 'wp_enqueue_scripts', 'mmk_enqueue_script', 999);
create a custom.css inside your theme directory. this CSS file will be applied to all pages except the one you specified.
update "123" inside if condition with page id where you don't want custom css.
The above code will enqueue additional css based on the condition. for more info https://developer.wordpress.org/reference/functions/wp_enqueue_style/ https://developer.wordpress.org/reference/functions/is_page/
-
This is overkill when the same can be achieved with a CSS rule alone. Commented Mar 10, 2021 at 7:17
-
1agree, but depends on the context. if you want to style an entire page that is different from the rest of the site, a separate CSS won't hurt if it is loaded only on that specific page.– MMKCommented Mar 10, 2021 at 9:22