- This topic has 11 replies, 2 voices, and was last updated 1 year, 8 months ago by David.
-
AuthorPosts
-
November 6, 2019 at 1:44 am #1054562Marcel
Hello,
I would ask what if there is an option to disable the style sheet for certain pages and posts. I use a child theme, and I would like to use custom HTML and CSS in some pages and posts. Even I use the Simple CSS plugin on those pages and apply custom CSS, the final result is weird. I know for sure there is a solution for that, please just give me a short guide. Thank you!November 6, 2019 at 5:30 am #1054725DavidStaffCustomer SupportHi there,
the correct way would be to save the styles in a separate style sheet and use the
wp_enqueue_scripts
hook:https://developer.wordpress.org/reference/functions/wp_enqueue_style/#comment-340
But this would require you to add a condition to where the code should be loaded.
Simplest method would be to use a Hook element and add a link to the style sheet in the WP_head for those specific pages:
<link rel="stylesheet" id="my-custom-style-sheet" href="full_url_to_my_custom_styles" type="text/css" media="all">
November 6, 2019 at 9:27 am #1055099MarcelDavid, thank you! So, to understand I should create a custom CSS file and store it on my child theme folder, where function.php and style.css already exist and name it according to the example given above (my-custom-style-sheet.css)? Image attached.
And, the full URL to my-custom-style-sheet would be: my-website.com/wp-content/themes/generatepress_child/my-custom-style-sheet.css?November 6, 2019 at 9:40 am #1055113DavidStaffCustomer SupportThats correct accept you change the
ID="my-custom-style-sheet"
and the style sheet file name to whatever you like as long as it is unique.November 8, 2019 at 2:28 am #1056665MarcelHello David,
Please guide me on this puzzle:
– should I put also my HTML into a Hook element and add a link to the style sheet in the WP_head for a specific page
OR
– just add a link to the style sheet in the WP_head for a specific page and put my HTML into that page (using a custom HTML Gutenberg block, for instance…)? Image here.
Thank you!November 8, 2019 at 2:36 am #1056671DavidStaffCustomer SupportThe second option
November 8, 2019 at 3:06 am #1056698MarcelPerfect. It is working. Now, if I would to use wp_enqueue_scripts (the correct way), should I include it on my function.php or using a hook? Could I have a hypothetical snippet from your side, let say for a style sheet with the ID=”my-custom-style-sheet” and adding a condition to where the code should be loaded, let say for the page with ID= 101? Thank you!
November 8, 2019 at 3:30 am #1056725DavidStaffCustomer Supportwp_enqueue_scripts would go in the function.php. Tom provides an example of conditional enqueue here:
November 12, 2019 at 9:45 pm #1061317MarcelThank you, David. Should I first use “wp_dequeue_style” first, for better results or it is not necessary?
November 13, 2019 at 5:25 am #1061652DavidStaffCustomer Supportwp_dequeue_style
is only required if you need to remove an existing style sheet. As you’re adding rather then replacing it is not required.August 5, 2022 at 3:32 pm #2304731MarcelThank you, David!
August 6, 2022 at 2:34 am #2304957DavidStaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.