How would you include a stylesheet
in both the block-editor
and on the front-end
if the page meets certain criteria?
In this case I want to include a stylesheet
called homepage.css
if the page is the home page only. I therefore did this:
add_action('enqueue_block_assets', function () { if(is_front_page()){ wp_enqueue_style( 'homepage-stylesheet', get_template_directory_uri() . '/homepage.css', ); } });
Whilst the code works on the front-end
it does not in the block-editor
as it appears that is_front_page()
cannot be determined until the page is rendered.
How would you go about this?
.home
rule and take advantage of the body classes WP adds to help with this. Otherwise what you want is impossible if you ever plan to use the template or site editors as there's no way to know from PHP since the user can click into another template and it would be brokenenqueue_block_assets
may not be the ideal action to do that on, and there are definitely disadvantages to this approach and additional complexity, it's not the silver bullet it first appears to be