web-dev-qa-db-fra.com

Comment changer la galerie insérée dans le message?

Lorsque j'insère une galerie dans un article de blog à l'aide du shortcode [gallery] ou simplement du bouton insert media, la galerie structurée par défaut de mon blog se trouve sous the_content(); fais le?

2
ilyo

Vous pouvez filtrer le shortcode de la galerie par défaut. Voici quelque chose que j'ai utilisé dans le passé.

add_filter( 'post_gallery', 'wpse_gallery', 10, 2 );

function wpse_gallery() {
    global $post;

    /* Orderby */
    if ( isset( $attr['orderby'] ) ) :
        $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
        if ( !$attr['orderby'] )
            unset( $attr['orderby'] );
    endif;

    /*
    * Extract default gallery settings
    */
    extract(shortcode_atts(array(
        'order'      => 'ASC',
        'orderby'    => 'menu_order ID',
        'id'         => $post->ID,
        'itemtag'    => 'dl',
        'icontag'    => 'dt',
        'captiontag' => 'dd',
        'columns'    => 3,
        'size'       => 'thumbnail',
    ), $attr));

    /*
    * Make sure $id is an integer
    */
    $id = intval( $id );

    /*
    * Get image attachments
    * If none, return
    */
    $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, leave the default WP settings
    * We're only worried about on-site presentation
    */
    if ( is_feed() ) {
        $output = "\n";
        foreach ( $attachments as $id => $attachment )
            $output .= wp_get_attachment_link( $id, $size, true ) . "\n";
        return $output;
    }

    $i = 0;

    /*
    * Remove the style output in the middle of the freakin' page.
    * This needs to be added to the header.
    * The width applied through CSS but limits it a bit.
    */

    /*
    * Open the gallery <div>
    */
    $output .= '<div id="gallery-'.$id.'" class="content gallery gallery-'.$id.'">'."\n";
    $output .= '<div id="thumbs" class="navigation">'."\n";
    $output .= '<ul class="thumbs noscript">'."\n";
    /*
    * Loop through each attachment
    */
    foreach ( $attachments as $id => $attachment ) :

        /*
        * Get the caption and title
        */
        $caption = esc_html( $attachment->post_excerpt, 1 );
        $title = esc_html( $attachment->post_title, 1 );
        $link = wp_get_attachment_image_src( $id, 'large' );
        $img = wp_get_attachment_image_src( $id, $size );

        /*
        * Open each gallery item
        */
        $output .= "\n\t\t\t\t\t<li class='gallery-item'>";
            $output .= '<a class="thumb" href="' .  wp_get_attachment_url( $id ) . '" title="' . $title . '">';
                $output .= '<img src="' . $img[0] . '" alt="' . $title . '" title="' . $title . '" />';
            $output .= '</a>';

        /*
        * If image caption is set
        */
        if ( $caption ) :
            $output .= "\n\t\t\t\t\t\t<div class='caption'>";
                $output .= $caption;
            $output .= "</div>";
        endif;

        /*
        * Close individual gallery item
        */
        $output .= "\n\t\t\t\t\t</li>";

    endforeach;

    /*
    * Close gallery and return it
    */

        $output .= '</ul><!--.thumbs-->'."\n";
        $output .= '</div><!--#thumbs-->'."\n";
    /*
    * Return out very Nice, valid XHTML gallery.
    */
    return $output;

}
6
Chris_O