【问题标题】:ACF no rows found using wordpress advanced custom field pluginACF 使用 wordpress 高级自定义字段插件找不到行
【发布时间】:2018-07-11 01:30:51
【问题描述】:

您好专家,我一直在尝试从我创建的字段中检索数据

字段名称是“ddw”,它的转发器和

它的子字段 op1 有很多行

但我仍然无法使用此代码检索任何行

<?php 
require_once 'wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
include_once 'wp-content/plugins/acf351/acf.php';
// check if the repeater field has rows of data
if( have_rows(get_field('ddw')) ):
    // loop through the rows of data
    while ( have_rows(get_field('ddw')) ) : the_row();

        // display a sub field value
       echo the_sub_field('op1');

    endwhile;
else :
    echo 'no rows found';
endif;
?>

它没有找到 row 。我希望每个帖子的所有行,特别是我希望将 http 链接放在数组上并循环遍历它。我已将此脚本放在 wp 目录中,它不是主题或模板文件夹。请帮助我在哪里做错了。在此先感谢

【问题讨论】:

  • 我会使用echo get_sub_fieldthe_sub_field 而不是echo the_sub_field
  • 我试过了,但仍然是“找不到行”,这样你就知道我不是在尝试获取一个帖子的元值,我正在尝试获取所有帖子的子字段“op1”行。
  • 好吧,我不是专家,但我熟悉 ACF 中继器插件。我还注意到您可能需要删除 'ddw' 上的 get_field... 即 if( have_rows('ddw') ): // 循环遍历数据行 while ( have_rows('ddw') ) : the_row( );
  • git-e-up 是对的,直接把'ddw'放在两个have_rows()中
  • 我试过了,但它仍然显示没有找到行。我认为 have_rows 应该显示特定帖子 id 的子字段行,但我试图从所有帖子中显示。

标签: php wordpress foreach advanced-custom-fields


【解决方案1】:

要使您的代码正常工作,它应该如下所示。 (将 $post_id 替换为您的帖子 id 变量)

<?php
require_once 'wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
include_once 'wp-content/plugins/acf351/acf.php';
// check if the repeater field has rows of data
if( have_rows('ddw' , $post_id) ):
    // loop through the rows of data
    while ( have_rows('ddw', $post_id) ) : the_row();

        // display a sub field value
       echo get_sub_field('op1');

    endwhile;
else :
    echo 'no rows found';
endif;
?>

您可以在此处找到适用于所有场景的代码示例,例如无循环或所有带有帖子 ID 的帖子。 尝试 https://www.advancedcustomfields.com/resources/code-examples/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 2019-04-11
    • 2018-03-25
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    相关资源
    最近更新 更多