duancai7568
2019-01-09 05:59
浏览 65
已采纳

在foreach循环中获取ACF字段数据 - wordpress

I have a custom image field for all pages with a specific page template (using ACF plugin).

I'm querying for these pages like so:

    $posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'page',
    'meta_key'      => '_wp_page_template',
    'meta_value'    => 'services-page.php'
));

Then I'm displaying pages with a foreach loop:

if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post );?>
//content goes here
<?php endforeach; ?> 
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Now I want to access the custom field to display inside the loop. But, below doesn't work. I'm guessing because ACF fields don't get appended to the post object.

//Does not work    
$image = $post -> services_block_image

ACF has the get_field() function, but what can I do to get the field for each of the posts from my original query? Found ACF docs to be rather confusing on this (goes without saying I'm a bit new to PHP).

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题