1. Inicio
  2. Base de conocimientos
  3. PREGUNTAS FRECUENTES
  4. ¿Cómo abrir imágenes en un lightbox?

¿Cómo abrir imágenes en un lightbox?

Puede utilizar la función oum_location_bubble_image para manipular la salida de la imagen de localización. Esto le permite añadir una función de lightbox, por ejemplo.

Ejemplo: Lightbox con WP Featherlight

Yo recomendaría utilizar el Plugin WP Featherlight para obtener un efecto lightbox en las imágenes. Y con el siguiente script PHP (añádelo al functions.php de tu tema o utiliza un plugin Code Snippet) puedes habilitar el efecto lightbox para las imágenes de ubicación. Añade la función data-featherlight="imagen" a las imágenes.

PHP
add_filter('oum_location_bubble_image', function ($image, $location) {
    // Define the data attribute to be added
    $data_attribute = 'data-featherlight="image"';

    // Add the data attribute to images that do not already have it
    $image = preg_replace('/<img(?![^>]*data-featherlight=)([^>]+)>/', '<img$1 ' . $data_attribute . '>', $image);

    return $image;
}, 10, 2);

Ejemplo: Añadir una clase personalizada a las imágenes

PHP
add_filter('oum_location_bubble_image', function ($image, $location) {
    // Define the class to be added
    $custom_class = 'lightbox';

    // Add the custom class to images that already have a class attribute
    $image = preg_replace('/<img(.*?)class="([^"]*)"(.*?>)/', '<img$1class="$2 ' . $custom_class . '"$3', $image);

    return $image;
}, 10, 2);
PHP
add_filter('oum_location_bubble_image', function ($image, $location) {
    // Wrap images inside <a class="lightbox" href="image-src">
    $image = preg_replace_callback(
        '/<img([^>]*?)src=["\'](.*?)["\'](.*?)>/',
        function ($matches) {
            $imgTag = '<img' . $matches[1] . 'src="' . $matches[2] . '"' . $matches[3] . '>';
            return '<a class="lightbox" href="' . $matches[2] . '">' . $imgTag . '</a>';
        },
        $image
    );

    return $image;
}, 10, 2);
¿Le ha resultado útil este artículo?

Artículos relacionados


ES