草庐IT

php - 整个组高级自定义字段的输出字段

coder 2024-04-11 原文

我正在尝试找到一种方法(使用 wordpress 插件 ACF)来输出整个字段组。

我有两个“字段组”,如果其中一个是“事件的”(带有一个复选框),我想将它们回显到我的模板中。

问题是,如何从一个“字段组”中获取所有字段数据的数组?

现在我正在尝试以下操作:

<?php

$group_ID = 37;

$fields = array();
$fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);

echo "<pre>";
print_r($fields);
echo "</pre>";

?>

它输出一个字段数组,但据我所知它不包含实际数据:

Array
(
    [0] => Array
        (
            [key] => field_537b79d92081f
            [label] => Active-1
            [name] => active-1
            [_name] => active-1
            [type] => true_false
            [order_no] => 0
            [instructions] => 
            [required] => 0
            [id] => acf-field-active-1
            [class] => true_false
            [conditional_logic] => Array
                (
                    [status] => 0
                    [rules] => Array
                        (
                            [0] => Array
                                (
                                    [field] => null
                                    [operator] => ==
                                )

                        )

                    [allorany] => all
                )

            [message] => 
            [default_value] => 0
            [field_group] => 37
        )

    [1] => Array
        (
            [key] => field_537b78a4c743e
            [label] => Title-1
            [name] => title-1
            [_name] => title-1
            [type] => text
            [order_no] => 1
            [instructions] => 
            [required] => 0
            [id] => acf-field-title-1
            [class] => text
            [conditional_logic] => Array
                (
                    [status] => 0
                    [rules] => Array
                        (
                            [0] => Array
                                (
                                    [field] => field_537b79d92081f
                                    [operator] => ==
                                    [value] => 1
                                )

                        )

                    [allorany] => all
                )

            [default_value] => 
            [placeholder] => 
            [prepend] => 
            [append] => 
            [formatting] => html
            [maxlength] => 
            [field_group] => 37
        )

    [2] => Array
        (
            [key] => field_537b78c6c743f
            [label] => Info-1
            [name] => info-1
            [_name] => info-1
            [type] => textarea
            [order_no] => 2
            [instructions] => 
            [required] => 0
            [id] => acf-field-info-1
            [class] => textarea
            [conditional_logic] => Array
                (
                    [status] => 0
                    [rules] => Array
                        (
                            [0] => Array
                                (
                                    [field] => field_537b79d92081f
                                    [operator] => ==
                                    [value] => 1
                                )

                        )

                    [allorany] => all
                )

            [default_value] => 
            [placeholder] => 
            [maxlength] => 
            [rows] => 
            [formatting] => br
            [field_group] => 37
        )

)

我想基本上执行以下操作(请原谅 sudo/损坏的代码,PHP 不是我的语言):

<?php

    $group_ID1 = 37;

    $fields = array($group_ID1);

    if ($fields1['active'] !== null) {
        echo "<h1>";
        echo $fields1['active']['text'];
        echo $fields1['title']['text'];
        echo $fields1['content']['text'];
        echo "<h1>";
    }

    $group_ID2 = 38;

    $fields2 = array($group_ID2);

    if ($fields2['active'] !== null) {
        echo "<h1>";
        echo $fields2['active']['text'];
        echo $fields2['title']['text'];
        echo $fields2['content']['text'];
        echo "<h1>";
    }

?>

我还可以获得如下特定字段数据:

<?php

    $field_key = "field_537b78c6c743f";
    $field = get_field_object($field_key);

    if($field) {
        echo "<pre>";
        print_r($field);
        echo "</pre>";
    }

?>

输出如下:

Array
(
    [key] => field_537b78c6c743f
    [label] => Info-1
    [name] => info-1
    [_name] => info-1
    [type] => textarea
    [order_no] => 2
    [instructions] => 
    [required] => 0
    [id] => acf-field-info-1
    [class] => textarea
    [conditional_logic] => Array
        (
            [status] => 0
            [rules] => Array
                (
                    [0] => Array
                        (
                            [field] => field_537b79d92081f
                            [operator] => ==
                            [value] => 1
                        )

                )

            [allorany] => all
        )

    [default_value] => 
    [placeholder] => 
    [maxlength] => 
    [rows] => 
    [formatting] => br
    [field_group] => 37
    [value] => testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest
)

但我真的需要为每个“字段组”调用每个字段键吗?一个“字段组”中不是所有字段的数组吗?

最佳答案

您可以获得属于一个组的所有字段的数组。函数是:

$acf_fields = acf_get_fields_by_id( $group_ID );

我们需要知道 ACF 字段组 ID。您可以在组编辑页面的 url 中找到它,例如:

http://site.ru/wp-admin/post.php?post=340&action=edit

在这种情况下,组 ID 是 340。如果您不想使用硬编码 ID(如果您的组不时更改),您可以使用组名(在此示例中组名 id Technic CPT)获取它):

global $wpdb;
$group_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = 'Technic CPT'" );

输出看起来像这样:

array(1) {
  [0] => array(29) {
    ["ID"] => int(366)
    ["key"] => string(19) "field_55f05295a59ca"
    ["label"] => string(29) "Gallery of technic"
    ["name"] => string(15) "technic_gallery"
    ["prefix"] => string(0) ""
    ["type"] => string(7) "gallery"
    ["value"] => NULL
    ["menu_order"] => int(6)
    ["instructions"] => string(155) "Field instructions"
    ["required"] => int(0)
    ["id"] => string(0) ""
    ["class"] => string(0) ""
    ["conditional_logic"] => int(0)
    ["parent"] => int(340)
    ["wrapper"] => array(3) {
      ["width"] => int(50)
      ["class"] => string(0) ""
      ["id"] => string(0) ""
    }
    ["_name"] => string(15) "technic_gallery"
    ["_input"] => string(0) ""
    ["_valid"] => int(1)
    ["min"] => string(0) ""
    ["max"] => int(10)
    ["preview_size"] => string(9) "thumbnail"
    ["library"] => string(3) "all"
    ["min_width"] => int(475)
    ["min_height"] => int(365)
    ["min_size"] => string(0) ""
    ["max_width"] => string(0) ""
    ["max_height"] => string(0) ""
    ["max_size"] => int(2)
    ["mime_types"] => string(0) ""
  }
}

关于php - 整个组高级自定义字段的输出字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23778726/

有关php - 整个组高级自定义字段的输出字段的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  3. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  4. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  5. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  6. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  7. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  8. ruby - 如何进行排列以有效地定制输出 - 2

    这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][

  9. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  10. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

随机推荐