使用高级自定义字段循环自定义帖子对象

时间:2014-04-08 21:15:14

标签: php wordpress advanced-custom-fields

我正在尝试以下内容,例如来自http://www.advancedcustomfields.com/resources/field-types/post-object/,但它只输出空div:

<?php $post_objects = get_field('project_experts');

if( $post_objects ): ?>
    <div class="row expert">
    <?php foreach( $post_objects as $post): ?>
    <?php setup_postdata($post); ?>
        <div class="mt-one-half">
            <h3><a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a></h3>
            <?php the_excerpt(); ?>
        </div>
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    </div>
<?php endif; ?>

然而,当我尝试<?php print_r( get_field('project_experts') ); ?>时,我知道信息就在那里:

Array ( [0] => Array ( [project_expert] => WP_Post Object ( [ID] => 763 [post_author] => 1 [post_date] => 2014-03-27 17:57:29 [post_date_gmt] => 2014-03-27 17:57:29 [post_content] =>

等等。

从数组中获取值的任何指针?

谢谢!

1 个答案:

答案 0 :(得分:1)

你关闭了。您只需要在get_field返回的数组中更深入一层。

<?php foreach( $post_objects as $array): ?>
    <?php foreach( $array as $obj): ?>

<?php setup_postdata($obj); ?>
    <div class="mt-one-half">
        <h3><a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a></h3>
        <?php the_excerpt(); ?>
    </div>
    <?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endforeach; ?>
//etc... as before