Created
March 31, 2023 08:49
-
-
Save FrancoStino/584c30fbe8abe2daba8277c1232c2f81 to your computer and use it in GitHub Desktop.
Add Featured Image background - WPBakery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'vc_after_mapping', 'add_background_image_option_to_row' ); | |
function add_background_image_option_to_row() { | |
// Aggiungi l'opzione di design | |
vc_add_param( 'vc_row', array( | |
'type' => 'checkbox', | |
'heading' => __( 'Usa immagine in evidenza della categoria come sfondo?', 'text-domain' ), | |
'param_name' => 'use_category_image_as_background', | |
'value' => array( __( 'Sì', 'text-domain' ) => 'yes' ), | |
'description' => __( 'Seleziona questa opzione per impostare l\'immagine in evidenza della categoria come sfondo della riga.', 'text-domain' ), | |
'group' => __( 'Opzioni Design' ), | |
) ); | |
// Aggiungi la funzione di background | |
add_filter( 'vc_shortcode_output', 'add_background_image_to_row', 10, 3 ); | |
} | |
function add_background_image_to_row( $output, $obj, $atts ) { | |
$row_id = $atts['el_id']; | |
// Verifica se l'opzione è stata selezionata | |
if ( isset( $atts['use_category_image_as_background'] ) && $atts['use_category_image_as_background'] == 'yes' ) { | |
// Verifica se siamo su una pagina di categoria prodotto | |
if ( is_product_category() ) { | |
// Ottieni l'immagine in evidenza della categoria | |
$category = get_queried_object(); | |
$image_id = get_term_meta( $category->term_id, 'thumbnail_id', true ); | |
$image_url = wp_get_attachment_url( $image_id ); | |
// Aggiungi il CSS per lo sfondo | |
$output .= '<style> .sc_layouts_hide_on_frontpage{ background-image: url(' . $image_url . '); background-size: cover; } </style>'; | |
} else { | |
// Ottieni l'immagine in evidenza della pagina | |
$image_id = get_post_thumbnail_id( get_the_ID() ); | |
$image_url = wp_get_attachment_url( $image_id ); | |
// Aggiungi il CSS per lo sfondo | |
$output .= '<style> .sc_layouts_hide_on_frontpage { background-image: url(' . $image_url . '); background-size: cover; } </style>'; | |
} | |
} | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment