目次
< 記事一覧

【WP】メディア一覧の画像を表示する方法

以下のパラメーターで記事ではなく、メディアにアップロードした画像を取得できる。

<?php
	$paged = get_query_var('paged') ? get_query_var('paged'): 1; //ページネーションが必要な場合は追加
	$args = array(
		'posts_per_page' => 10,
		'paged' => $paged,  //ページネーションが必要な場合は追加
		'orderby' => 'date',
		'order' => 'DESC',
		'post_type' => 'attachment',  // post_typeは【attachment】を指定してね!
		'post_mime_type' => 'image',
		'post_status' => 'inherit'  // これ必須!
	);
	$the_query = new WP_Query($args);
	if ( $the_query->have_posts() ):
?>
<ul>
<?php while ( $the_query->have_posts() ): $the_query->the_post(); ?>
	<li>
		<div class="box-news-list-img object-fit-img">
			<img src="<?php echo wp_get_attachment_url($post->ID); ?>">
		</div>
	</li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<div class="post" style="text-align: center;">
	<p class="post_title">現在準備中です。</p><br>
	<p>現在準備しておりますので、お待ちください。</p>
 </div>
<?php endif; wp_reset_query(); ?>

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です