PHP WordPress - 高级自定义字段 - 在Pro版本中应用过滤器

问题描述 投票:0回答:1

我正在构建一个WP插件,并根据其网站上的文档集成了ACF(免费版):https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/

我还使用一系列过滤器根据位置设置加载ACF字段:

$this->loader->add_filter( 'acf/location/rule_types', $plugin_admin, 'acf_location_rules_types' );
$this->loader->add_filter( 'acf/location/rule_values/cpt', $plugin_admin, 'acf_location_rules_values_cpt' );
$this->loader->add_filter( 'acf/location/rule_match/cpt', $plugin_admin, 'acf_location_rules_match_cpt', 10, 3 );

此代码基于这个简单的插件:https://github.com/lukechapman/custom-post-template-support-for-acf/blob/master/acf-custom-post-template-support.php

一切都完美无缺,但自从我升级到ACF专业版以后,这已经完全停止了。

我似乎找不到任何特定于为ACF pro添加过滤器的资源,只有基本版本。我调试了它,可以确认路径是正确的,日志中没有错误,我还在第一个回调函数acf_location_rules_types()中添加了一些调试代码,而且根本没有调用它。

加载过滤器的脚本正在运行,但它就像ACF Pro中不再存在过滤器选项一样。

我哪里错了?

编辑

我也试过这样一个非常简单的实现,它也没有被调用:

add_filter('acf/location/rule_types', function ($rules) {
    error_log('acf/location/rule_types debug');
    return false;
});
php wordpress advanced-custom-fields
1个回答
0
投票
<?php
// functions.php
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices ) {
    $choices['Post']['post-type-has-taxonomy'] = __('Post Type has Taxonomy');
    return $choices;
}
add_filter( 'acf/location/rule_values/post-type-has-taxonomy', 'acf_location_rules_values_has_taxonomy' );
function acf_location_rules_values_has_taxonomy( $choices ) {
    return array_merge($choices, get_taxonomies());
}
add_filter('acf/location/rule_match/post-type-has-taxonomy', 'acf_location_rules_match_has_taxonomy', 10, 3);
function acf_location_rules_match_has_taxonomy( $match, $rule, $options )
{
    if ($rule['operator'] == '==') {
        $match = is_object_in_taxonomy($options['post_type'], $rule['value']);
    } elseif ($rule['operator'] == '!=') {
        $match = !is_object_in_taxonomy($options['post_type'], $rule['value']);
    }
    return $match;
}
© www.soinside.com 2019 - 2024. All rights reserved.