Support » Fixing WordPress » custom-background in home page only

  • I’m trying to have a custom background using add_theme_support in functions file that is only shown on the home page.

    add_theme_support( 'custom-background' ) works when used by itself, but I can’t get it to work in a condition. This is my code which includes echo statements for testing:

    function custom_bg() {
        if(is_front_page()){ 
    
        	echo "you are at front page"; 
    
            add_theme_support( 'custom-background' );
    
        }else{
            echo "you are NOT at front page";
        }
    }
    add_action( 'wp', 'custom_bg' );

    The function seems to work, since the echo statements behave as expected. I’m hooking to ‘wp’ so that the function gets executed after wordpress knows that the page will be the front page. What am I missing or doing wrong?

    In Reading Settings I have static page with the correct front page. I’m using a front-page.php template file. And I’m using underscores starter theme.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Bet Hannon

    (@bethannon1)

    Support Team Volunteer Coordinator

    Seems to me that it would be far simpler to do this purely through CSS. Sorry I’m no help with the PHP, but is there some reason you can’t just target the background of the home page?

    It sure is far simpler to do it through CSS for me as the developer, but not for the end user. In fact, I currently have it with CSS as a temporary solution, but the owner of the site should be able to change the picture from the admin panel without touching any code.

    I’m trying to make it work with WordPress’ built in features without having to use a plug in. And I wouldn’t want to make the image load on all pages and hide it with CSS on all pages except the home page.

    😉

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘custom-background in home page only’ is closed to new replies.