从ACF转发器字段获取meta_key

时间:2020-01-29 15:38:27

标签: wordpress loops advanced-custom-fields repeater meta-key

是否可以通过转发器字段内容遍历wordpress帖子?

我有一个活动,您可以通过转发器字段(ACF)添加活动,我想显示每个活动的所有日期。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

是的,您可以使用meta_query来标识具有该键的帖子,然后循环访问转发器字段。看起来应该像下面这样。

ACF Repeater Fields

$args = array(   
    'meta_query' => array(
        array(
            'key'     => 'event_date',    //Or whatever your        
            'compare' => 'EXISTS',
        ),
    ),
);
// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {

       // check if the repeater field has rows of data
       if( have_rows('repeater_field_name') ){

           // loop through the rows of data
           while ( have_rows('repeater_field_name') ) : the_row();

              // display a sub field value
              the_sub_field('sub_field_name');

           endwhile;

       else {

       // no rows found

      }
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();