当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムや WordSlack #docs チャンネルでお知らせください。</p>
万が一、当サイトで重大な問題を発見した際などは、フォーラムや WordSlack #docs チャンネルでお知らせください。</p>
関数リファレンス/get post gallery
提供: WordPress Codex 日本語版
特定の投稿に対してギャラリーの有無をチェックし、もし存在すれば最初のギャラリーを返す関数です。
パラメータ
- $post
- (mixed) (オプション) 投稿 ID または投稿オブジェクト。
- 初期値: 0
- $html
- (真または偽) (オプション) HTML を返すか、データを返すかの指定。
- 初期値: true
戻り値
- (string / array)
- ギャラリーのデータと拡張されたショートコードから解析されたソース。
使い方
<?php /* The loop */ while ( have_posts() ) : the_post(); if ( get_post_gallery() ) : echo get_post_gallery(); endif; endwhile; ?>
用例
Output each image in a gallery with our own custom image class when using data output and not HTML
<?php /* The loop */ while ( have_posts() ) : the_post(); if ( get_post_gallery() ) : $gallery = get_post_gallery( get_the_ID(), false ); /* Loop through all the image and output them one by one */ foreach( $gallery['src'] AS $src ) { ?> <img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" /> <?php } endif; endwhile; ?>
データ出力の例:
array (size=3) 'link' => string 'file' (length=4) 'ids' => string '423,424,425,426' (length=15) 'src' => array (size=4) 0 => string 'http://www.yoursite.com/wp-content/uploads/2013/10/1296136694836-150x150.jpg' (length=85) 1 => string 'http://www.yoursite.com/wp-content/uploads/2013/10/1315981706292-150x150.jpg' (length=85) 2 => string 'http://www.yoursite.com/wp-content/uploads/2013/10/fate-stay-night-446-wall1200-150x150.jpg' (length=100) 3 => string 'http://www.yoursite.com/wp-content/uploads/2013/10/GpTq3-150x150.jpg' (length=77)
改訂履歴
- 3.6.0 にて導入されました。
ソースファイル
get_post_gallery() は wp-includes/media.php
にあります。
関連項目
get_post_galleries(), get_post_galleries_images()
最新英語版: WordPress Codex » Function Reference/get post gallery (最新版との差分)