X

Come modificare il comportamento predefinito dello sfondo

Snippet di IsItWP

Vuoi modificare il comportamento predefinito dello sfondo? Questo snippet ti permetterà di personalizzare qualsiasi parametro dello sfondo desideri, nonché l'elemento a cui è applicato.

Istruzioni

Tutto quello che devi fare è aggiungere questo codice al file functions.php del tuo tema o in un plugin specifico per il sito:

// Activate custom background and set callback function
if ( function_exists( 'add_theme_support' ) ) {
    $defaults = array(
        'default-color' => '000000',
        'default-image' => get_template_directory_uri() . '/img/background.png',
        'wp-head-callback' => 'my_theme_background_cb',
        'admin-head-callback'    => '',
        'admin-preview-callback' => ''
    );
    add_theme_support( 'custom-background', $defaults );
}
// Callback function to alter custom background behavior
function my_theme_background_cb() {
    $background = get_background_image();
    $color = get_background_color();
 
    if ( ! $background && ! $color )
        return;
 
    $style = $color ? "background-color: #$color;" : '';
 
    if ( $background ) {
        $image = " background-image: url('$background');";
 
        $repeat = get_theme_mod( 'background_repeat', 'repeat' );
        if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
                $repeat = 'repeat';
        $repeat = " background-repeat: $repeat;";
 
        $position = get_theme_mod( 'background_position_x', 'left' );
        if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
                $position = 'left';
        $position = " background-position: top $position;";
 
        $attachment = get_theme_mod( 'background_attachment', 'scroll' );
        if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
                $attachment = 'scroll';
        $attachment = " background-attachment: $attachment;";
 
        $style .= $image . $repeat . $position . $attachment;
    }
?>
    <!-- You can set any class or id target here -->
    <style type="text/css" id="custom-background-css">;
        .main { <?php echo trim( $style ); ?> }
    </style>
<?php } 

Nota: Se questa è la prima volta che aggiungi snippet di codice in WordPress, consulta la nostra guida su come copiare / incollare correttamente snippet di codice in WordPress, in modo da non rompere accidentalmente il tuo sito.

Se ti è piaciuto questo snippet di codice, prendi in considerazione la possibilità di consultare Recensione di WPBakery Page Builder.

Commenti   Lascia una risposta

Aggiungi un commento

Siamo lieti che tu abbia scelto di lasciare un commento. Tieni presente che tutti i commenti sono moderati secondo la nostra normativa sulla privacy e tutti i link sono nofollow. NON utilizzare parole chiave nel campo del nome. Avviamo una conversazione personale e significativa.

Checklist per il lancio di WordPress

La Guida Definitiva per il Lancio di WordPress

Abbiamo raccolto tutti gli elementi essenziali della checklist per il lancio del tuo prossimo sito web WordPress in un comodo ebook.
Sì, Inviami l'eBook Gratuito!