WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get background image

Description

Retrieve the custom background image for the current active theme.

Usage

<?php get_background_image(); ?>

Parameters

None.

Return Values

(string) 
Returns a string containing the absolute link to the custom background image for the current active theme

Examples

Use Theme Background Image as Fallback if No Featured Image Exists

This example could be used to detect whether the current Page/Post has a Featured Image set – if so, it will use the Featured Image as the page background, if not it will use the current active theme's default background image. As is, this should be used in the <head> of the page template, just after the call to wp_head():

<?php 

    // declare $post global if used outside of the loop
    global $post;

    // check to see if the theme supports Featured Images, and one is set
    if (current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID )) {
            
        // specify desired image size in place of 'full'
        $page_bg_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
        $page_bg_image_url = $page_bg_image[0]; // this returns just the URL of the image

    } else {
        // the fallback – our current active theme's default bg image
        $page_bg_image_url = get_background_image();
    }

    // And below, spit out the <style> tag... ?>
    <style type="text/css" id="custom-background-css-override">
        body.custom-background { background-image: url('<?php echo $page_bg_image_url; ?>'); }
    </style>

Notes

Change Log

Since: 3.0.0

Source File

get_background_image() is located in wp-includes/theme.php.

Related

background_image(), background_color(), get_background_color()

This article is marked as in need of editing. You can help Codex by editing it.
This page is marked as incomplete. You can help Codex by expanding it.
See also index of Function Reference and index of Template Tags.