Web production note

 【更新日 :

【WordPress】foreachで各記事の情報を取得し表示させる

Category:
WordPress

foreachで各記事の情報を取得し表示させるサンプルです。

<?php
	$args = array(
		//取得先の設定
		'post_type' => 'post',
		//全件取得
		'posts_per_page' => -1,
		//公開記事のみ取得
		'post_status' => array(
			'publish'
		 )
	);
	$post_items = get_posts( $args );
  if($post_items):
?>	
  <?php foreach( $post_items as $key => $post_val ):
	  //foreachの中に各記事の取得タグを追加
	
	  //ID取得
	  $this_post_id = $post_val->ID;
  ?>

	  <p><a href="<?php echo get_permalink($this_post_id); ?>"><?php echo get_the_title($this_post_id); ?></a></p>
	  <p>投稿記事内のACFカスタムフィールドを出力: <?php the_field('ACFフィールド名',$this_post_id); ?></p>

  <?php endforeach;?>
<?php endif;?>

関連リンク

目次 を閉じる