【问题标题】:How can I get the name of field group in CFS?如何在 CFS 中获取字段组的名称?
【发布时间】:2021-11-23 02:01:31
【问题描述】:

在 Wordpress 中,有一个名为 Custom Field Suite 的插件: https://nl.wordpress.org/plugins/custom-field-suite/

问题?我无法定位特定字段组中的所有字段。我可以看到字段组是 post 类型,但我似乎无法定位它。

API 也没有关于它的信息: https://mgibbs189.github.io/custom-field-suite/api.html

我的目标是获取特定字段组中的所有字段,而不必一一查询所有字段。

有人让它工作吗?

编辑:

如果我对那里的内容进行 var 转储,我找不到字段组的名称。这就是问题的全部。

$list = CFS()->get();
print_r ($list);

Array ( [tester] => test
[location] => nomee) 

虽然我的字段组的名称 = "Hello"

【问题讨论】:

    标签: php wordpress api field


    【解决方案1】:

    当我们在 CFS 插件中创建一组字段时,我们在技术上创建了一个名为 cfs 的自定义帖子类型

    我们还将创建三个自定义字段:

    1. cfs_extras - 这是存储字段组设置的位置
    2. cfs_rules - 显示字段组的条件,应在哪个自定义帖子类型或分类中显示
    3. cfs_fields - 组的其他字段存储在这里

    如果我们需要为一组字段查找字段,首先我们必须进行这样的查询

    $my_fields_group_name = 'test';
    
    $query = get_posts( 'no_found_rows=true&fields=ids&post_type=cfs&s=' . $my_fields_group_name );
    
    $my_fields_group_post_id = $query[0];
    

    然后从自定义字段“cfs_fields”中获取字段的名称

    $my_fields = get_post_meta($my_fields_group_post_id , "cfs_fields", true);
    
    print_r($my_fields);
    

    现在我们有了所有自定义字段键,我们可以获取这些字段的值

    $current_post_id = get_the_id();
    foreach($my_fields as $my_field) {
    $custom_field_key = $my_field ['name'];
    
    $custom_field_value = get_post_meta($current_post_id , $custom_field_key, true);
    echo $custom_field_key . ' - ' . $custom_field_value;
    echo '<br>';
    }
    

    这是所有的代码组合

    $my_fields_group_name = 'test';
    
    $query = get_posts( 'no_found_rows=true&fields=ids&post_type=cfs&s=' . $my_fields_group_name );
    
    $my_fields_group_post_id = $query[0];
    
    $my_fields = get_post_meta($my_fields_group_post_id , "cfs_fields", true);
    
    print_r($my_fields);
    echo '<br>';
    
    $current_post_id = get_the_id();
    
    foreach($my_fields as $my_field) {
    
    $custom_field_key = $my_field ['name'];
    
    $custom_field_value = get_post_meta($current_post_id , $custom_field_key, true);
    echo $custom_field_key . ' - ' . $custom_field_value;
    echo '<br>';
    
    }
    

    更新 如果不想每次都查询,可以获取自定义post fields group的post_id

    $my_fields_group_name = 'test';
    
    $query = get_posts( 'no_found_rows=true&fields=ids&post_type=cfs&s=' . $my_fields_group_name );
    
    $my_fields_group_post_id = $query[0];
    

    并在代码中直接使用这个id

    $my_id = 28;
    $my_fields = get_post_meta($my_id , "cfs_fields", true);
    
    $current_post_id = get_the_id();
    
    foreach($my_fields as $my_field) {
    
    $custom_field_key = $my_field ['name'];
    
    $custom_field_value = get_post_meta($current_post_id , $custom_field_key, true);
    echo $custom_field_key . ' - ' . $custom_field_value;
    echo '<br>';
    
    }
    

    【讨论】:

    • 太棒了!但是,这可行:是否可以缩短此时间?喜欢:我确切地知道我需要选择哪些字段组,而不是查询所有字段组,我可能需要这样做 4 或 5 次才能选择某些字段组。有没有更高效/漂亮的方法?
    • 尝试更新代码
    • 谢谢:有没有办法我们可以为每个字段(而不是字段组)指定一个类名?我认为 CFS 不允许我们按字段发送类,但这对我来说很重要。有办法吗?
    • 有什么方法可以去掉 $custom_field_value 输出的
      ?即使您删除了 echo
      ,仍然有一个似乎来自 $custom_field_value 的中断。
    • 有什么可以帮忙的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多