WordPressのギャラリーの表示部分をCSSで自由にしたい!!
下記コードをfunctions.php に追加してください。
<?php
/* デフォルトの gallery ショートコードを削除 */
remove_shortcode('gallery', 'gallery_shortcode');
/* gallery ショートコードをカスタム */
add_shortcode('gallery', 'custom_gallery_shortcode');
function custom_gallery_shortcode($attr) {
$post = get_post();
static $instance = 0;
$instance++;
if ( ! empty( $attr['ids'] ) ) {
if ( empty( $attr['orderby'] ) )
$attr['orderby'] = 'post__in';
$attr['include'] = $attr['ids'];
}
$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 ? $post->ID : 0,
'itemtag' => 'li',
'icontag' => 'figure', //画像のタグ
'captiontag' => 'figcaption', //キャプションのタグ
'columns' => 1,
'size' => 'thumbnail', //サムネイルサイズ
'include' => '',
'exclude' => '',
'link' => ''
), $attr, 'gallery'));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($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) ) {
$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);
$icontag = tag_escape($icontag);
$valid_tags = wp_kses_allowed_html( 'post' );
if ( ! isset( $valid_tags[ $itemtag ] ) )
$itemtag = 'dl';
if ( ! isset( $valid_tags[ $captiontag ] ) )
$captiontag = 'dd';
if ( ! isset( $valid_tags[ $icontag ] ) )
$icontag = 'dt';
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
$size_class = sanitize_html_class( $size );
$gallery_div = "<ul id='$selector' class='gallery'>\n";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$image = wp_get_attachment_image_src($id, $size);
$full = wp_get_attachment_image_src($id, 'large');
$image_output = '<a href="'.$full[0].'" data-lightbox="lightbox" data-title="'.wptexturize($attachment->post_excerpt).'"><img src="'.$image[0].'" alt="'.wptexturize($attachment->post_excerpt).'" class="over"></a>';
$image_meta = wp_get_attachment_metadata( $id );
$orientation = '';
if ( isset( $image_meta['height'], $image_meta['width'] ) )
$orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
$output .= "<{$itemtag} class='gallery-item'>\n";
$output .= "<{$icontag} class='{$orientation}'>
$image_output\n";
$output .= "</{$icontag}>";
$output .= "</{$itemtag}>\n";
}
$output .= "</ul>\n";
return $output;
}
使っていない処理とかも残ってたりしますが、このコードを入れることによって、下記のようなシンプルなコードが出力されるようになります。
<ul id='gallery-1' class='gallery'>
<li class='gallery-item'>
<figure class='portrait'>
<a href="http://example.com/wp-content/uploads/dummy.jpg" data-lightbox="lightbox" data-title="タイトル">
<img src="http://example.com/wp-content/uploads/thumbnail.jpg" alt="タイトル" class="over"></a>
</figure>
</li>
<li class='gallery-item'>
<figure class='landscape'>
<a href="http://example.com/wp-content/uploads/dummy.jpg" data-lightbox="lightbox" data-title="タイトル">
<img src="http://example.com/wp-content/uploads/thumbnail.jpg" alt="タイトル" class="over"></a>
</figure>
</li>
</ul>今回は、Lightboxを使うようにタグを調整しています。
出力部分のコードは、97行目あたりを弄れば自由に吐き出すことが可能です。
※このコードを追加すると、管理画面上の「ギャラリーの設定」は機能しなくなります。
サムネイルサイズやカラム数を設定したところ、この関数で固定になります。
サムネイルサイズを変えるときには、37行目の値を変更してください。
カラムなどのレイアウトについては、基本CSSで調整することなります。



コメント