Come personalizzare l'output dello shortcode della galleria immagini WP da un plugin?

20 nov 2010, 15:57:31
Visualizzazioni: 44.7K
Voti: 17

Stiamo sviluppando un plugin che mostra i post e vogliamo anche visualizzare la galleria di immagini quando viene utilizzata in un post. Tuttavia, abbiamo bisogno di limitare il numero di foto visualizzate. È possibile farlo?

0
Tutte le risposte alla domanda 2
2
37

Ci sono due modi per affrontare questo problema, ma entrambi implicano la creazione di una funzione che fa sostanzialmente la stessa cosa della funzione esistente dello shortcode della galleria...

Puoi scegliere tra..

  1. Agganciarti a post_gallery e manipolare i dati in entrata (puoi usare la funzione dello shortcode della galleria come base per il filtro se necessario)
  2. Annullare la registrazione dello shortcode della galleria e registrarne uno nuovo con le modifiche (anche qui puoi usare la funzione esistente come base se necessario)

Ho fatto qualcosa di simile in questo thread, e lo cito solo perché prenderò lo stesso approccio per l'esempio che segue.

Esempio di filtro per lo shortcode della galleria

add_filter( 'post_gallery', 'my_post_gallery', 10, 2 );
function my_post_gallery( $output, $attr) {
    global $post, $wp_locale;

    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}";

    $output = apply_filters('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 -->
        <div id='$selector' class='gallery galleryid-{$id}'>");

    $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='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;
}

Modifica questa funzione per imporre le restrizioni che preferisci (è solo una base)...

Puoi trovare l'hook che sto usando in wp-includes/media.php all'interno della funzione di callback dello shortcode della galleria (vedi riga 763).

http://core.trac.wordpress.org/browser/tags/3.0.1/wp-includes/media.php#L745

Spero che sia d'aiuto.. :)

21 nov 2010 01:17:30
Commenti

grazie per il suggerimento. Ho cercato questo filtro **** per 2 ore. +1

kaiser kaiser
6 mag 2011 03:33:31

Sembra che in WP 4.2.2 'orderby' => 'menu_order ID' non funzioni più?

Peanuts Peanuts
13 mag 2015 15:17:28
0
-2

Puoi specificare il numero di immagini per galleria. Questo può essere configurato tramite la scheda della galleria nel popup dei media nell'editor degli articoli. Dopo aver già inserito la galleria, penso che tu possa modificarla editando la galleria nell'editor visuale.

21 nov 2010 01:02:31