duancai7568 2019-01-09 05:59
浏览 67

在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条回答 默认 最新


      相关推荐 更多相似问题


      • ¥15 关于学生成绩的一个系统(语言-c语言)
      • ¥60 matlab传染病模型分段
      • ¥15 关于#matlab#的问题,如何解决?
      • ¥15 51单片机汇编语言Keil5基础问题!
      • ¥15 CygwinPortable 中 NumPy 无法安装
      • ¥15 vivado如何支持多维打包数组模式
      • ¥15 请问第13题到底应该怎么做
      • ¥20 数据可视化综合运用 导入npz文件以及读取内容
      • ¥15 倒计时汇编语言RTC实时时钟
      • ¥15 CCS安装出现Failed to create the part's controls