トランスビット
トランスビットの開発ノート Webサイト制作に役立つTipsやトラブルシューティングなどの備忘録

WordPressのギャラリーのカラム数を変更したり色々

世の中の皆さんはGW中ですね。
私は、仕事以外での想定外のイベント事……っていうかトラブルが多すぎて、その分回り回ってぐるぐるして、今年は半分GW、半分仕事って感じです。

さて、結局仕事では使わなかったけど、今後使うかもしれない程度に便利そうなのでメモ。
function.phpに記述。

1/*********************************************************************************
2// ギャラリーの設定を変更
3*********************************************************************************/
4 
5function amethyst_gallery_atts( $out, $pairs, $atts ) {
6 
7    $atts = shortcode_atts( array(
8        'columns' => '4',
9        //'size' => 'thumbnail',
10         ), $atts );
11 
12    $out['columns'] = $atts['columns'];
13    $out['size'] = $atts['size'];
14 
15    return $out;
16 
17}
18add_filter( 'shortcode_atts_gallery', 'amethyst_gallery_atts', 10, 3 );
19/*********************************************************************************
20// アイキャッチ画像がある場合はアイキャッチ画像を、ない場合は記事のギャラリーに入っている一番最初の画像を表示
21*********************************************************************************/
22 
23function print_post_image($size = 'thumbnail'){
24  if ( has_post_thumbnail() ) {
25    the_post_thumbnail($size);
26  } else {
27    $attachments = get_children(array(
28      'post_parent' => get_the_ID(),
29      'post_type' => 'attachment',
30      'post_mime_type' => 'image',
31      'order' => 'DESC'
32      ));
33    if(!empty($attachments)){
34      $img = array_shift($attachments);
35      echo wp_get_attachment_image($img->ID ,$size);
36    }
37  }
38}

Filed under: