[英]Cannot get ACF from custom post type post
我有一個名為“members”的自定義帖子類型,它連接了一個ACF字段。 我試着獲得該領域的內容。 但是當我嘗試時,我只獲得常規的帖子對象。 未連接ACF字段。
這是我正在嘗試,但只獲得post對象。
`
$featuredmembers = get_field('featured_member');
global $post;
//$featuredmembers has a field named "featured". That's the field I want.
$posts = get_posts([
'post_type' => 'members',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title'
]);
foreach ($featuredmembers as $post) {
print_r($post['featured']->ID);
echo get_field('featured');
}
`
嘗試在foreach循環中的get_field / the_field調用中傳遞post id
:
foreach ($featuredmembers as $post) {
// the_field('featured', $post->ID);
echo get_field('featured', $post->ID);
}
如果它是轉發器字段,您可以使用:
foreach ($featuredmembers as $post) {
the_repeater_field('featured', $post->ID);
}
我認為這可能是使用$post
變量的foreach
循環。 這將覆蓋頁面的主$post
變量,並在foreach循環查看錯誤的帖子后調用任何get_field
函數。
嘗試將這些變量重命名為$post
以外的其他變量
您是否嘗試過使用WP_Query來訪問post_id?
$args = array(
'post_type' => 'members',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title'
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ) :
$current_post->the_post();
$post_id = get_the_ID();
$featured = esc_html(get_post_meta($post_id, 'featured_member', true));
endwhile;
wp_reset_postdata();
else :
esc_html_e( 'no members found', 'text-domain' );
endif;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.