固定ページでカスタムポストデータを取得
wordpress 固定ページ カスタムポスト
基本
◆ 条件
カスタムポストスラッグ:sample_post
表示件数:5件
<?php
$args = array(
'post_type' => 'sample_post',
'posts_per_page' => 5,
);
$custom_posts = get_posts($args);
foreach($custom_posts as $custom_post) :
?>
<?php echo get_permalink($custom_post->ID); //パーマリンクを表示するとき ?>
<?php echo get_the_title($custom_post->ID); //タイトルを表示するとき ?>
<?php echo get_field('acfで設定したfieldname', $custom_post->ID); //acfフィールドを表示するとき ?>
<?php
endforeach;
?>
タクソノミーを等含めた場
◆ 条件
カスタムポストスラッグ:sample_post
カスタムタクソノミー:sample_post_category
カスタムタクソノミーのターム:news、event
表示件数:5件
$args = array(
'post_type' => 'sample_post',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy'=>'sample_post_category',
'terms'=>array( 'news', 'event'),
'field'=>'slug',
'operator'=>'IN'
),
'relation' => 'AND'
)
);
$custom_posts = get_posts($args);
foreach($custom_posts as $custom_post) :
?>
<?php echo get_permalink($custom_post->ID); //パーマリンクを表示するとき ?>
<?php echo get_the_title($custom_post->ID); //タイトルを表示するとき ?>
<?php echo get_field('acfで設定したfieldname', $custom_post->ID); //acfフィールドを表示するとき ?>
<?php
endforeach;
?>