情報を追記している場合はありますが、古い情報を訂正はしていませんので、公開年月日を参照してください。プラグイン・タグ、いずれもワードプレス・PHPのバージョン等によって動作しない場合もあります。
添付ファイルの一覧を作成する方法。工務店や建築家のサイトで施工写真の一覧から詳細ページにリンクするために書いたコード例です。
*image_query は書き方の一例なので、他の書き方でもOKです。
<?php $query = new WP_Query(
array(
'post_type' => '投稿タイプ',
'posts_per_page' => -1,
'fields' => 'ids'
)
);
$image_query = new WP_Query(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'post_parent__in' => $query->posts,
'order' => 'DESC'
~ 画像にカスタムフィールド値をつけて、ここで指定も可能 / カテゴリー分けしたい場合はパラメータで
)
);
if( $image_query->have_posts() ){
while( $image_query->have_posts() ) {
$image_query->the_post();
$imgurl = wp_get_attachment_url( get_the_ID() );
$attachment_id = attachment_url_to_postid( $imgurl );
$post = get_post($attachment_id);
$pid = $post->post_parent;
$link = get_permalink($pid);
~ その他必要な情報を取得 ~
画像の一覧を作成するには、flush_rewrite_rules()でattachment のスラッグを変更/アーカイブ有りに変更してアーカイブを作る方法もあり、この場合はページャーをつけられます。