https://www.tilo-lutzke.de/wordpress
WordPress

WordPress Gallery – dl und dt ersetzen

Sep / 2016

Euch stört die standardmäßige Vorgabe von WordPress, einer Gallery Kolumnen vorzugeben und dass diese dann mittels den Tags dl und dt ausgegeben wird. Für nachfolgenden Code einfach in eure functions.php ein und modifiziert die Ausgabe der Gallery nach Euren Wünschen (in diesem Beispiel als „unsorted List“)

add_shortcode( 'gallery', 'custom_gallery_shortcode' );

function custom_gallery_shortcode( $attr = array(), $content = '' )
{
        $attr['itemtag']        = "li";
        $attr['icontag']        = "";
        $attr['captiontag']     = "p";

        // Run the native gallery shortcode callback:    
        $html = gallery_shortcode( $attr );

        // Remove all tags except a, img,li, p
        $html = strip_tags( $html, '<a><img><li><p>' );

        // Some trivial replacements:
        $from = array(  
            "class='gallery-item'", 
            "class='gallery-icon landscape'", 
            'class="attachment-thumbnail"',
            'a href=', 
        );              
        $to = array( 
            '',
            '',
            'class="tnggallery"', 
            'a class="lightbox" href=', 
        );
        $html = str_replace( $from, $to, $html );

        // Remove width/height attributes:
        $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );

        // Wrap the output in ul tags:
        $html = sprintf( '<ul class="gallery">%s</ul>', $html );

        return $html;
}