[英]Cannot get ACF from custom post type post
I have a custom post type called "members", which has an ACF field connected. 我有一个名为“members”的自定义帖子类型,它连接了一个ACF字段。 I try to get the content of that field.
我试着获得该领域的内容。 But when I try, I only get the regular post object.
但是当我尝试时,我只获得常规的帖子对象。 Not the ACF fields connected.
未连接ACF字段。
Here is what i am trying, but only getting the post object. 这是我正在尝试,但只获得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');
}
` `
Try passing the post id
in the get_field / the_field call within the foreach loop: 尝试在foreach循环中的get_field / the_field调用中传递
post id
:
foreach ($featuredmembers as $post) {
// the_field('featured', $post->ID);
echo get_field('featured', $post->ID);
}
If it's a repeater field you can use: 如果它是转发器字段,您可以使用:
foreach ($featuredmembers as $post) {
the_repeater_field('featured', $post->ID);
}
I think this might be down the foreach
loops using a variable of $post
. 我认为这可能是使用
$post
变量的foreach
循环。 This will override the main $post
variable of the page, and any get_field
function calls after the foreach loop will be looking at the wrong post. 这将覆盖页面的主
$post
变量,并在foreach循环查看错误的帖子后调用任何get_field
函数。
Try renaming those variables to something other than $post
尝试将这些变量重命名为
$post
以外的其他变量
Have you tried using WP_Query to get access to the post_id? 您是否尝试过使用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.