Cum să reparați manual codul galeriei WordPress folosind PHP în functions.php?
S-a discutat de multe ori despre faptul că WordPress generează un cod destul de defectuos pentru funcția integrată de galerie.
Acesta este codul de bază responsabil pentru generarea galeriei (în /wp-includes/media.php):
function gallery_shortcode($attr) {
global $post;
static $instance = 0;
$instance++;
// Permite plugin-urilor/temelor să suprascrie șablonul implicit al galeriei
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
// Ne încredem în input-ul autorului, dar măcar să ne asigurăm că arată ca o declarație orderby validă
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>
<!-- vezi gallery_shortcode() în 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;
}
Ce aș dori să repar?
[1] Codul de mai sus introduce stiluri CSS direct în articol. Vreau să opresc acest lucru, deoarece pot adăuga ușor același cod CSS în fișierul style.css.
[2] Vreau să dezactivez codul care afișează caption-urile imaginilor sub miniaturi. Vreau ca caption-urile să fie afișate doar pe paginile de atașamente, și nu în articol.
[3] Codul menționat mai sus adaugă două elemente <br style="clear: both;">
după codul galeriei. Aș dori să dezactivez și acest lucru, deoarece pot folosi "margin" în codul CSS pentru asta.
Aș dori să realizez cele "TREI" lucruri de mai sus folosind cod PHP în fișierul functions.php
, deoarece editarea fișierelor de bază nu este recomandată.
Sper că cineva poate fi de ajutor. (Nu știu să programez, așa că vă rog să fiți cât mai clari posibil.) Mulțumesc!
RELEVANT: Fișierul sursă de analizat este /wp-includes/media.php
(aici este versiunea trunk -- căutați funcția gallery_shortcode
).
După cum s-a menționat anterior, eliminarea shortcode-ului și readăugarea acestuia nu este compatibilă cu alte plugin-uri care modifică galeriile, așa că în schimb puteți utiliza hook-ul de filtrare post_gallery
și același cod din funcția gallery_shortcode
, dar cu propriile modificări. De exemplu, am comentat părțile pe care nu le doriți:
function fix_my_gallery_wpse43558($output, $attr) {
global $post;
static $instance = 0;
$instance++;
/**
* Vom elimina acest lucru deoarece nu dorim o buclă infinită aici
*/
// Permite plugin-urilor/temelor să înlocuiască șablonul implicit al galeriei.
//$output = apply_filters('post_gallery', '', $attr);
// Avem încredere în inputul autorului, dar să ne asigurăm că arată ca o comandă orderby validă
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 ) )
/**
* acesta este CSS-ul pe care doriți să îl eliminați
* #1 în întrebare
*/
/*
$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>
<!-- see 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}>";
/*
* Aceasta este partea de caption, așa că o voi comenta
* #2 în întrebare
*/
/*
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" />';
}
/**
* acesta este tag-ul br suplimentar pe care doriți să îl eliminați, așa că îl schimbăm doar în tag-ul de închidere div
* #3 în întrebare
*/
/*$output .= "
<br style='clear: both;' />
</div>\n";
*/
$output .= "</div>\n";
return $output;
}
add_filter("post_gallery", "fix_my_gallery_wpse43558",10,2);

De fapt, în comentariul existent, vezi codul lung sub EDIT. Face același lucru :)

Primul bloc de cod care începe cu function my_own_gallery($output, $attr) { ...

Vezi editarea lui badlearner mai jos
Puteți elimina shortcode-ul implicit și să vă creați propriul. Astfel (în functions.php):
remove_shortcode( 'gallery' );
function my_own_gallary() {
// Cod pentru galerie
}
add_shortcode( 'gallery' , 'my_own_gallary' );
Cea mai simplă metodă de a modifica shortcode-ul este să-l copiați și să-l lipiți în functions.php, apoi să schimbați numele funcției în ceva precum my_own_gallary
și să începeți editarea.
EDITARE
După cum a menționat goldenapples în comentarii: Există un filtru pentru shortcode-ul gallery, așadar nu este nevoie să eliminați mai întâi shortcode-ul.
exemplu pe care îl puteți utiliza în functions.php (ieșirea este ca shortcode-ul implicit gallery, astfel încât îl puteți modifica).
function my_own_gallery($output, $attr) {
global $post;
static $instance = 0;
$instance++;
// Ne bazăm pe inputul autorului, dar să ne asigurăm că arată ca o comandă orderby validă
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>
<!-- vezi gallery_shortcode() în 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);
Explicație: În shortcode-ul definit de WordPress veți vedea:
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
Aceasta înseamnă că dacă un filtru este aplicat și returnează ceva, acesta va fi utilizat (returnat), altfel funcția continuă (shortcode-ul implicit).
Pentru a adăuga un filtru utilizați funcția add_filter. Primul argument este tag-ul filtrului (în acest caz 'post_gallery'), al doilea este funcția de adăugat (funcția care va returna output-ul personalizat al galeriei).
Așadar, acest cod va afișa "test" pentru shortcode-ul [gallery]:
function my_own_gallery($output, $attr) {
return 'test';
}
add_filter("post_gallery", "my_own_gallery",10,2);
În exemplul meu de mai jos veți vedea add_filter pentru a crea shortcode-ul implicit cu propriul dvs. cod editabil. Puteți edita acest cod sau puteți începe de la zero, după preferințe.
(EDITARE de Otto: Cele de mai sus au fost corectate de Otto. @RobVermeer lipsea primul parametru al filtrului și nu a făcut add_filter corect. Filtrul post_gallery este metoda corectă de a face acest lucru. Eliminarea shortcode-ului și readăugarea lui (așa cum a încercat badlearner mai jos) nu este recomandată, deoarece este incompatibilă cu alte plugin-uri care modifică galeriile.)
Adăugat/Editat de badlearner:
Metoda de filtrare pentru modificarea codului galeriei (în /wp-includes/media.php) utilizând functions.php, așa cum a fost furnizată de @RobVermeer, nu pare să funcționeze corect (vezi comentariile acestui răspuns).
Dar primul răspuns al lui @RobVermeer (adică, înainte de prima editare), care anulează înregistrarea shortcode-ului gallery și înregistrează un nou shortcode gallery, a funcționat. Iată codul și Vă rugăm să nu ezitați să editați sau să adăugați un răspuns dacă există o metodă mai bună.
Următorul este codul care trebuie adăugat în fișierul functions.php al temei dvs.:
<?php
remove_shortcode( 'gallery' );
add_shortcode( 'gallery' , 'my_own_gallary' );
function my_own_gallary($attr) {
global $post;
static $instance = 0;
$instance++;
// Permite plugin-urilor/temelor să înlocuiască șablonul implicit al galeriei.
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
// Ne bazăm pe inputul autorului, dar să ne asigurăm că arată ca o comandă orderby validă
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;
}
?>

și care va fi noul shortcode pentru galerie? [gallery]
în sine sau altceva?

[gallery]
într-adevăr. Dacă copiați și lipiți asta în fișierul function.php, veți vedea exact aceeași ieșire în articolele dumneavoastră. Dar dacă editați codul, îl puteți personaliza.

De fapt, nu este nevoie să eliminați întregul shortcode și să-l adăugați din nou. Dacă priviți în partea de sus a funcției de galerie din WordPress, există un filtru numit 'post_gallery'. Dacă acest filtru returnează ceva, WordPress îl va folosi ca ieșire pentru galerie în locul ieșirii sale proprii. Așadar, adăugați marcajul dvs. ca filtru pe 'post_gallery'.

@goldenapples Am trecut cu vederea acest aspect. Mulțumesc, aceasta este într-adevăr soluția corectă.

am să fur asta. urăsc marcajele galeriei și mai ales ale capturilor...

@goldenapples poți fi un pic mai clar? (Nu știu să codez.) Poate răspunsul tău? te rog? Mulțumesc.

@RobVermeer dacă ce a spus 'goldenapples' are sens pentru tine, poți explica și tu asta prin editarea răspunsului tău? Mulțumesc.

Mai multe informații în răspunsul meu actualizat, sper că acest lucru vă ajută.

@RobVermeer Se pare că nu funcționează pe postările cu galerii multiple. Adică... când introduc galerii din postări sau pagini existente într-o nouă postare folosind [gallery id="n"]
, galeriile nu sunt afișate. În schimb, văd doar aceeași imagine a noii postări. Vă rugăm să vedeți această pagină de exemplu: http://ow.ly/9ivOY.

@RobVermeer Acesta este codul original al galeriei din /wp-includes/media.php : http://pastebin.com/JStF2V41 ... Și acesta este codul pe care îl folosesc în functions.php : http://pastebin.com/1Q0iuvBn ... Puteți, vă rog, să verificați dacă fac ceva greșit? (Căutați tagurile <!--REMOVED CODE HERE-->
și <!--CHANGED CODE HERE-->
în ultimul.)

@RobVermeer NU! Te rog să cauți <!--REMOVED CODE HERE-->
și <!--CHANGED CODE HERE-->
în cod și să le compari în ambele documente. Vei observa diferența.

@RobVermeer Chiar și codul implicit pe care l-ai furnizat nu pare să funcționeze.

Ei bine, încă este prezent în noua versiune de WordPress începând cu 3.8 Iată remedierea pe care am făcut-o pentru a elimina acel lucru, folosind același cod de mai sus, dar am adăugat câteva linii
// Elimină shortcode-ul implicit pentru galerie
remove_shortcode( "gallery" );
// Adaugă shortcode personalizat pentru galerie
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.">";
/* Adăugat <dd> aici pentru a repara eroarea de validare */
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;
}

Te rog să adaugi o explicație adecvată la răspunsurile tale pentru a respecta standardele de calitate ale [wordpress.se] - pentru început citește [answer].
