ACF repeater field usage

时间:2015-08-18 作者:rudtek

我有一个自定义的post类型“event”,它有一个转发器字段“event\\u session”,该字段有4个子字段“session\\u start”、“session\\u title”、“session\\u description”、“session\\u speaker”(与CPT speaker的post对象关系)。

我正在尝试创建一个将加载子字段的页面。这就是我所尝试的:

<?php

    $args = array(
        \'post_type\' => \'event\'
    );

    $the_query = new WP_Query( $args );
?>


<!-- WP_Query WordPress loop -->
<?php

// check if the repeater field has rows of data
if( have_rows(\'event_session\') ):

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

        // display a sub field value
        the_sub_field(\'session_start\');

    endwhile;

else :

    // no rows found

endif;

?>
问题是我无法从CPT speaker中提取关系数据。如何将标题和公司名称(来自“speaker”的字段)输入到此页面?

新代码:

<?php

    $args = array(
        \'post_type\' => \'event\'
    );

    $the_query = new WP_Query( $args );
?>


<!-- WP_Query WordPress loop -->
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php if(get_field(\'event_session\')): ?>
        <?php while(has_sub_field(\'event_session\')): ?>
                <strong><?php the_sub_field(\'session_start\'); ?>  </strong><?php the_sub_field(\'session_title\'); ?><br>
<?php   

$eventspeakers = get_sub_field("session_speaker");
if ($eventspeakers && count($eventspeakers)>0)
{
    foreach ($eventspeakers as $eventspeaker)
    {
$speaker = get_post($eventspeaker);
echo\'speaker is\'.$speaker->post_title; 


    }        
}   
?>
        <?php endwhile; ?>
    <?php endif; ?>

<?php endwhile; else: ?>

    <!-- Displayed if no posts or pages are available -->
    <p>There are no posts or pages here!</p>

<?php endif; ?> 
这会导致拉取演讲者帖子标题,但会重复多次。一个事件如下所示:

8: 上午00点注册和;Breakfastspeaker isChristian Kochspeaker isspeaker isspeaker isspeaker isspeaker isspeaker is2015speaker isspeaker is2015speaker isspeaker is2015speaker isspeaker is2015speaker isspeaker is2015speaker isspeaker is2015speaker Is2015

1 个回复
最合适的回答,由SO网友:jammypeach 整理而成

简单使用get_sub_field("session_speaker"); 函数检索该数据。正如您所说,这是一个关系字段,它将返回array 属于post objects 根据the docs.

为了从每个说话者那里获取数据,您需要遍历生成的数组。示例:

$speakers = get_sub_field("session_speaker");
if ($speakers && count($speakers)>0)
{
    foreach ($speakers as $speaker)
    {
        echo $speaker->ID; //$speaker is a post object//
    }        
}

结束

相关推荐

ACF repeater field usage - 小码农CODE - 行之有效找到问题解决它

ACF repeater field usage

时间:2015-08-18 作者:rudtek

我有一个自定义的post类型“event”,它有一个转发器字段“event\\u session”,该字段有4个子字段“session\\u start”、“session\\u title”、“session\\u description”、“session\\u speaker”(与CPT speaker的post对象关系)。

我正在尝试创建一个将加载子字段的页面。这就是我所尝试的:

<?php

    $args = array(
        \'post_type\' => \'event\'
    );

    $the_query = new WP_Query( $args );
?>


<!-- WP_Query WordPress loop -->
<?php

// check if the repeater field has rows of data
if( have_rows(\'event_session\') ):

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

        // display a sub field value
        the_sub_field(\'session_start\');

    endwhile;

else :

    // no rows found

endif;

?>
问题是我无法从CPT speaker中提取关系数据。如何将标题和公司名称(来自“speaker”的字段)输入到此页面?

新代码:

<?php

    $args = array(
        \'post_type\' => \'event\'
    );

    $the_query = new WP_Query( $args );
?>


<!-- WP_Query WordPress loop -->
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php if(get_field(\'event_session\')): ?>
        <?php while(has_sub_field(\'event_session\')): ?>
                <strong><?php the_sub_field(\'session_start\'); ?>  </strong><?php the_sub_field(\'session_title\'); ?><br>
<?php   

$eventspeakers = get_sub_field("session_speaker");
if ($eventspeakers && count($eventspeakers)>0)
{
    foreach ($eventspeakers as $eventspeaker)
    {
$speaker = get_post($eventspeaker);
echo\'speaker is\'.$speaker->post_title; 


    }        
}   
?>
        <?php endwhile; ?>
    <?php endif; ?>

<?php endwhile; else: ?>

    <!-- Displayed if no posts or pages are available -->
    <p>There are no posts or pages here!</p>

<?php endif; ?> 
这会导致拉取演讲者帖子标题,但会重复多次。一个事件如下所示:

8: 上午00点注册和;Breakfastspeaker isChristian Kochspeaker isspeaker isspeaker isspeaker isspeaker isspeaker is2015speaker isspeaker is2015speaker isspeaker is2015speaker isspeaker is2015speaker isspeaker is2015speaker isspeaker is2015speaker Is2015

1 个回复
最合适的回答,由SO网友:jammypeach 整理而成

简单使用get_sub_field("session_speaker"); 函数检索该数据。正如您所说,这是一个关系字段,它将返回array 属于post objects 根据the docs.

为了从每个说话者那里获取数据,您需要遍历生成的数组。示例:

$speakers = get_sub_field("session_speaker");
if ($speakers && count($speakers)>0)
{
    foreach ($speakers as $speaker)
    {
        echo $speaker->ID; //$speaker is a post object//
    }        
}

相关推荐