Come correggere manualmente il codice della galleria WordPress usando PHP in functions.php?
Si è parlato molte volte del fatto che WordPress genera del codice non ottimale per la funzione integrata della galleria.
Questo è il codice core responsabile dell'output della galleria (in /wp-includes/media.php):
function gallery_shortcode($attr) {
global $post;
static $instance = 0;
$instance++;
// Consente a plugin/temi di sovrascrivere il template predefinito della galleria
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
// Verifichiamo che l'input dell'autore sembri un valido orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = "
<style type='text/css'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>
<!-- vedi gallery_shortcode() in wp-includes/media.php -->";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
}
Cosa vorrei correggere?
[1] Il codice sopra riportato inserisce lo stile CSS direttamente nel post. Vorrei impedirlo, dato che posso facilmente aggiungere lo stesso codice CSS nel mio file style.css.
[2] Voglio disabilitare il codice che mostra le didascalie delle immagini sotto le miniature. Voglio che le didascalie vengano mostrate solo nelle pagine degli allegati e non nel post.
[3] Il codice menzionato aggiunge due elementi <br style="clear: both;">
dopo il codice della galleria. Vorrei anche disabilitare questo, poiché posso usare il "margin" nel codice CSS.
Vorrei ottenere le suddette "TRE" cose utilizzando del codice PHP nel file functions.php
, poiché non è consigliato modificare i file core.
Spero che qualcuno possa essere d'aiuto. (Non so programmare, quindi vi prego di essere il più chiari possibile.) Grazie!
RILEVANTE: Il file sorgente da guardare è /wp-includes/media.php
(qui c'è la versione trunk -- cerca la funzione gallery_shortcode
).
Come è stato menzionato prima, rimuovere lo shortcode e riaggiungerlo non è compatibile con altri plugin che modificano le gallerie, quindi invece si utilizza l'hook filter post_gallery
e lo stesso codice della funzione gallery_shortcode
ma con le proprie modifiche. Ad esempio, ho commentato le parti che non si desidera mantenere:
function fix_my_gallery_wpse43558($output, $attr) {
global $post;
static $instance = 0;
$instance++;
/**
* Rimuoveremo questo poiché non vogliamo un loop infinito qui
*/
// Permetti a plugin/temi di sovrascrivere il template predefinito della galleria.
//$output = apply_filters('post_gallery', '', $attr);
// Ci fidiamo dell'input dell'autore, ma almeno assicuriamoci che sembri un'istruzione orderby valida
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) )
/**
* questo è il CSS che vuoi rimuovere
* #1 nella domanda
*/
/*
$gallery_style = "
<style type='text/css'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>
<!-- vedi gallery_shortcode() in wp-includes/media.php -->";
*/
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
/*
* Questa è la parte della didascalia, quindi la commenteremo
* #2 nella domanda
*/
/*
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}*/
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
/**
* questo è il br extra che vuoi rimuovere, quindi lo cambiamo con solo il tag di chiusura del div
* #3 nella domanda
*/
/*$output .= "
<br style='clear: both;' />
</div>\n";
*/
$output .= "</div>\n";
return $output;
}
add_filter("post_gallery", "fix_my_gallery_wpse43558",10,2);

In realtà, nel commento esistente, vedi il codice lungo sotto EDIT. Fa la stessa cosa :)

Il primo blocco di codice che inizia con function my_own_gallery($output, $attr) { ...

Guarda la modifica di badlearner qui sotto
Puoi rimuovere lo shortcode predefinito e crearne uno tuo. In questo modo (nel tuo functions.php):
remove_shortcode( 'gallery' );
function my_own_gallary() {
// Codice della galleria
}
add_shortcode( 'gallery' , 'my_own_gallary' );
Il modo più semplice per modificare lo shortcode è copiarlo e incollarlo nel tuo functions.php, cambiare il nome della funzione in qualcosa come my_own_gallary
e iniziare a modificare.
MODIFICA
Come goldenapples ha sottolineato nei commenti: C'è un filtro per lo shortcode della galleria, quindi non è necessario rimuovere prima lo shortcode.
Ecco un esempio che puoi usare nel tuo functions.php (l'output è come lo shortcode predefinito della galleria, quindi puoi modificarlo).
function my_own_gallery($output, $attr) {
global $post;
static $instance = 0;
$instance++;
// Ci fidiamo dell'input dell'autore, ma almeno assicuriamoci che sembri un'istruzione orderby valida
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = "
<style type='text/css'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>
<!-- vedi gallery_shortcode() in wp-includes/media.php -->";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
}
add_filter("post_gallery", "my_own_gallery",10,2);
Spiegazione: Nello shortcode definito da WordPress vedrai:
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
Questo significa che se viene applicato un filtro e restituisce qualcosa, quello verrà utilizzato (restituito), altrimenti la funzione continua (shortcode predefinito).
Per aggiungere un filtro usi la funzione add_filter. Il primo argomento è il tag del filtro (in questo caso 'post_gallery'), il secondo la funzione da aggiungere (la funzione che restituirà l'output personalizzato della tua galleria).
Quindi questo restituirà "test" per lo shortcode [gallery]:
function my_own_gallery($output, $attr) {
return 'test';
}
add_filter("post_gallery", "my_own_gallery",10,2);
Nell'esempio qui sotto nella modifica vedrai l'add_filter per creare lo shortcode predefinito con il tuo codice modificabile. Puoi modificarlo o ricominciare da zero come preferisci.
(MODIFICA di Otto: Quanto sopra è stato ora corretto da Otto. @RobVermeer mancava il primo parametro del filtro e non aveva fatto l'add_filter correttamente. Il filtro post_gallery è il modo corretto per farlo. Rimuovere lo shortcode e riaggiungerlo (come provato da badlearner qui sotto) non è consigliabile poiché è incompatibile con altri plugin che modificano le gallerie.)
Aggiunto/Modificato da badlearner:
Il metodo del filtro per modificare il codice della galleria (in /wp-includes/media.php) usando functions.php, come fornito da @RobVermeer, non sembra funzionare correttamente (vedi i commenti di questa risposta).
Ma la prima risposta di @RobVermeer (cioè, prima della prima modifica), che annulla la registrazione dello shortcode della galleria e ne registra uno nuovo, ha funzionato. Ed ecco il codice, e Sentiti libero di modificare o aggiungere una risposta se c'è un modo migliore.
Il seguente è il codice che deve essere aggiunto nel file functions.php del tuo tema:
<?php
remove_shortcode( 'gallery' );
add_shortcode( 'gallery' , 'my_own_gallary' );
function my_own_gallary($attr) {
global $post;
static $instance = 0;
$instance++;
// Permetti a plugin/temi di sovrascrivere il template predefinito della galleria.
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
// Ci fidiamo dell'input dell'autore, ma almeno assicuriamoci che sembri un'istruzione orderby valida
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = "
<style type='text/css'>
#{$selector} .gallery-item {
width: {$itemwidth}%;
}
</style>";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '';
}
$output .= "
<div style='clear:both;'></div>
</div>\n";
return $output;
}
?>

e qual è il nuovo shortcode per la galleria? [gallery]
stesso o qualcos'altro?

proprio [gallery]
. Se copi e incolli questo nel tuo file function.php vedrai esattamente lo stesso output nei tuoi post. Ma se modifichi il codice puoi personalizzarlo a tuo piacimento.

In realtà non è necessario rimuovere completamente lo shortcode e riaggiungerlo. Se guardi nella parte superiore della funzione gallery di WordPress, c'è un filtro chiamato 'post_gallery'. Se quel filtro restituisce qualcosa, WordPress lo utilizzerà come output della galleria invece del proprio output. Quindi basta aggiungere il tuo markup come filtro su 'post_gallery'.

@goldenapples Ho trascurato questo aspetto. Grazie, questa è davvero la strada da percorrere.

Rubo questa idea. Odio il markup della galleria E soprattutto quello delle didascalie...

@goldenapples puoi essere un po' più chiaro? (Non so programmare.) Forse la tua risposta? per favore? Grazie.

@RobVermeer se quello che ha detto 'goldenapples' ha senso per te, puoi spiegarlo anche tu modificando la tua risposta? Grazie.

Un po' più di informazioni nella mia risposta aggiornata, spero sia utile.

@RobVermeer Non sembra funzionare con post che hanno più gallerie. Cioè... quando inserisco gallerie di post o pagine esistenti in un nuovo post usando [gallery id="n"]
, le gallerie non vengono visualizzate. Invece vedo solo la stessa immagine del nuovo post. Per favore guarda questa pagina come esempio: http://ow.ly/9ivOY.

@RobVermeer Questo è il codice originale della galleria da /wp-includes/media.php : http://pastebin.com/JStF2V41 ... E questo è quello che sto usando nel mio functions.php : http://pastebin.com/1Q0iuvBn ... Puoi per favore controllare se sto facendo qualcosa di sbagliato? (Cerca i tag <!--REMOVED CODE HERE-->
e <!--CHANGED CODE HERE-->
nel secondo.)

@RobVermeer NO! Per favore cerca <!--REMOVED CODE HERE-->
e <!--CHANGED CODE HERE-->
nel codice e confrontali in entrambi i documenti. Noterai la differenza.

@RobVermeer Anche il codice predefinito che hai fornito non sembra funzionare.

@RobVermeer Ho modificato la tua risposta, per favore vedi se puoi aiutare.

beh, è ancora presente nella nuova versione di WordPress a partire dalla 3.8 ecco la correzione che ho apportato per rimuoverlo, ho utilizzato lo stesso codice sopra ma ho aggiunto un paio di righe
remove_shortcode( "gallery" );
add_shortcode( "gallery" , "my_own_gallary" );
function my_own_gallary( $attr ) {
global $post;
static $instance = 0;
$instance++;
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' ) {
return $output;
}
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ( 'RAND' == $order ) {
$orderby = 'none';
}
if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) ) {
return '';
}
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-".$instance;
$gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = "
<style type='text/css'>
#".$selector." .gallery-item {
width: ".$itemwidth."%;
}
</style>";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery galleryid-".$id." gallery-columns-".$columns." gallery-size-".$size_class."'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<".$itemtag." class='gallery-item'>";
$output .= "
<".$icontag." class='gallery-icon'>
$link
</".$icontag.">";
/* aggiunto il <dd> qui per correggere l'errore di validazione */
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<".$captiontag." class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</".$captiontag.">";
} else {
$output .= "
<".$captiontag." class='wp-caption-text gallery-caption' style='display:none;'></".$captiontag.">";
}
$output .= "</".$itemtag.">";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '';
}
$output .= "
<div style='clear:both;'></div>
</div>\n";
return $output;
}

Per favore, aggiungi una spiegazione adeguata alle tue risposte per rispettare gli standard di qualità di [wordpress.se] - per iniziare leggi la [risposta].
