I have a small code to inject some CSS to the single post. And it has an option in the Customizer to enable/disable the CSS. My code is similar to this:

add_action( 'wp_head', function() {
    if ( ! get_theme_mod( 'enable_custom_css' ) ) {
    if ( is_singular() ) {
        echo '<style>.site-header{ background-image: url(http://placehold.it/300);</style>';
} );

The problem is no matter I turn on or off the settings enable_custom_css, the custom CSS is always not outputted when previewing in the Customizer.

I dig a little bit deeper in the code and found that: when previewing in the Customizer, the function is_singular always returns false.

Do you know any way to make is_singular() work? Thanks

  • 1
    Do a print_r on get_theme_mod( 'enable_custom_css' ) and see if it has any value or not. – CodeMascot Jan 11 '17 at 10:32
  • Are you always loading the homepage in the customizer preview? You can navigate to a singular post on the frontend and click Customize in the admin bar to load that URL into the customizer preview initially. You can also navigate around the site inside of the customizer preview to navigate to a singular template. In short, is_singular() should behave as expected. It is returning true for me. – Weston Ruter Jan 12 '17 at 8:26

Your Answer

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

Browse other questions tagged or ask your own question.