web-dev-qa-db-ja.com

投稿に挿入されているギャラリーを変更する方法

[gallery]ショートコードまたは単にinsert mediaボタンを使用してブログ投稿にギャラリーを挿入すると、the_content();を通じてブログのデフォルトの構造化ギャラリーが表示されます。ただし、ギャラリーには多くのインラインCSSやその他の削除したいものがあります。やる?

2
ilyo

デフォルトのギャラリーのショートコードをフィルタリングできます。これは私が過去に使ったことがあります。

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