【问题标题】:Parse post object inside repeater field in ACF/Wordress在 ACF/Wordpress 中解析转发器字段内的帖子对象
【发布时间】:2022-01-16 13:19:52
【问题描述】:

我试图从转发器字段内的帖子对象中获取标题等。

    $classes = get_field('classes'); //repeater field containing a sub field named "class" (post object).

    <?php foreach($classes as $class) : ?>
      <?php 
        echo $class; //returns post objects as arrays.
        echo $class['title']; //returns nothing.
        echo $class['post_title']; //returns nothing.
      ?>
    <?php endforeach; ?>

返回什么:

[class] => WP_Post Object
    (
        [ID] => 57
        [post_author] => 1
        [post_date] => 2021-12-07 23:55:28
        [post_date_gmt] => 2021-12-07 23:55:28
        [post_content] => fffdfdf
        [post_title] => testa
        [post_excerpt] => dfdsgdsgf
        etc.....
    )

那么我如何获得帖子标题等?

【问题讨论】:

  • 不,由于某种原因这不起作用。
  • 如果有意义的话,post 对象是 Woo 商务产品。

标签: wordpress advanced-custom-fields


【解决方案1】:

根据 ACF 文档,这是循环遍历中继器字段并加载子字段值的方式:ACF | Repeater

// Check rows exists.
if( have_rows('classes') ):

    // Loop through rows.
    while( have_rows('classes') ) : the_row();

        // Load sub field value.
        $product = get_sub_field('class');

        // Access product info.
        $product_id   = $product->get_id();
        $product_name = $product->get_name();

    // End loop.
    endwhile;

// No value.
else :
    // Do something...
endif;

List of methods to access product information.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 2017-02-22
    • 1970-01-01
    • 2021-11-30
    • 2019-03-30
    • 2021-12-10
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多