I use TwentyTwent theme and want to change its background colour by page IDs.

I'm not sure but when I check the functions.php, I see below lines which are related to background colours:

    // Custom background color.
            'default-color' => 'f5efe0',

    // Add the background option.
    $background_color = get_theme_mod( 'background_color' );
    if ( ! $background_color ) {
        $background_color_arr = get_theme_support( 'custom-background' );
        $background_color     = $background_color_arr[0]['default-color'];
    $editor_color_palette[] = array(
        'name'  => __( 'Background Color', 'twentytwenty' ),
        'slug'  => 'background',
        'color' => '#' . $background_color,

Is there any simple way to change the background colour according to Page's ID?


  • How will you know which colour? Is it random? Or being stored in post meta?
    – Tom J Nowell
    Commented Jan 24, 2020 at 10:08
  • @TomJNowell I normally set the default background colour (#f5efe0) in the customization settings. However, I would like to change that colour into white (#ffffff) by Page IDs. (Actually trying to change it for 8 or 9 pages only if it is possible.) Commented Jan 24, 2020 at 10:18
  • 1
    If you know the IDs/slugs and know they will never change, this can be done with CSS alone, no PHP or JS changes needed, by using the page specific CSS classes added to the body tag
    – Tom J Nowell
    Commented Jan 24, 2020 at 12:36

2 Answers 2


The CSS is generated on the file inc/custom-css.php, the background color is set on line 76:

$background = sanitize_hex_color_no_hash( get_theme_mod( 'background_color' ) );

So you can take advantage of the theme_mod_{$name} filter, which changes the value of the get_theme_mod($name) function, by adding this to your functions.php file:

    function( $value ) {
        $custom_bg_page_ids = array( 18, 19, 20 );
        $custom_bg_color = 'ca2d2d';
        global $post;
        if ( in_array( $post->ID, $custom_bg_page_ids ) ) {
            return $custom_bg_color;
        return $value;
  • 1
    This will not work as there is no global $post value set at this moment. Commented Jan 24, 2020 at 11:54
  • Although my website is WP multi-site, this solution works well! Thank you both @Himad and Vitauts Stočka for your help! Appreciate that! Commented Jan 24, 2020 at 12:10
  • @VitautsStočka The global var $post is set before this filter call, which means that it'll be already defined.
    – Himad
    Commented Jan 24, 2020 at 12:55
  • @Himad For some reason on my test server '$post' it is not set at this point. Tried this solution myself as well as copied your code. But if it works for you - fine. Commented Jan 24, 2020 at 13:20

Not the most elegant solution, but you can use something like this

add_action('wp_footer', function() {
    $post = get_post();
    if ($post && $post->ID == ... do your post selection here) {
        body {
            background-color: #fff !important;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.