WordPressのギャラリーのカラム数を変更したり色々
世の中の皆さんはGW中ですね。
私は、仕事以外での想定外のイベント事……っていうかトラブルが多すぎて、その分回り回ってぐるぐるして、今年は半分GW、半分仕事って感じです。
さて、結局仕事では使わなかったけど、今後使うかもしれない程度に便利そうなのでメモ。
function.phpに記述。
/*********************************************************************************
// ギャラリーの設定を変更
*********************************************************************************/
function amethyst_gallery_atts( $out, $pairs, $atts ) {
$atts = shortcode_atts( array(
'columns' => '4',
//'size' => 'thumbnail',
), $atts );
$out['columns'] = $atts['columns'];
$out['size'] = $atts['size'];
return $out;
}
add_filter( 'shortcode_atts_gallery', 'amethyst_gallery_atts', 10, 3 );
/*********************************************************************************
// アイキャッチ画像がある場合はアイキャッチ画像を、ない場合は記事のギャラリーに入っている一番最初の画像を表示
*********************************************************************************/
function print_post_image($size = 'thumbnail'){
if ( has_post_thumbnail() ) {
the_post_thumbnail($size);
} else {
$attachments = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'DESC'
));
if(!empty($attachments)){
$img = array_shift($attachments);
echo wp_get_attachment_image($img->ID ,$size);
}
}
}
Filed under: Wordpress