[英]ACF filter to exclude all categories from select
我正在为 WordPress 使用“前端提交专业版”和“ACF(非专业版)插件。我正在使用这些插件为我的用户制作前端帖子创建者。
我有 200 多个类别,所以我想让我的用户更容易地使用 select 类别。 我将创建多个 forms 并且每个表单都会有几个类别可供用户选择。
现在我使用下面的过滤器从 forms 中排除一些类别。
add_filter('acf/fields/taxonomy/query/name=kathgories', 'exclude_categories', 10, 2);
function exclude_categories( $args, $field ) {
global $uncategorized_id;
$args['exclude'] = array(290,287,283,289,281,291,286,280,284,279); //the IDs of the excluded terms
return $args;
}
因为我有很多类别我不能排除上面代码中的200个类别太难了。
所以我想要一个过滤器,它会排除所有类别,只包含我想在每个表单中显示的 5-10 个类别。 我没有这样做的知识,所以我问是否有人可以提供帮助。
我还希望每个过滤器仅适用于一种形式。 我需要一些方法将过滤器链接到正确的表单。(可能通过链接或表单名称)
用过滤器解决了。
add_action( 'init', 'get_term_ids' );
function get_term_ids() {
global $uncategorized_id;
$u = get_term_by( 'slug', 'uncategorized', 'product_cat' );
$uncategorized_id = $u->term_id;
}
add_filter('acf/fields/taxonomy/query/name=kathgories', 'exclude_categories', 10, 2);
function exclude_categories( $args, $field ) {
global $uncategorized_id;
$args['exclude'] = array($uncategorized_id); //the IDs of the excluded terms
return $args;
}
回答我的问题。
我在上面的过滤器中将“排除”更改为“包含”,它似乎完全符合我的要求。
它只显示我在表格中给出的类别。
对于那些在“选择”中显示类别的人,请使用以下代码。
add_filter('acf/fields/taxonomy/query/name=kathgories', 'include_categories', 10, 2);
function include_categories( $args, $field ) {
global $uncategorized_id;
$args['include'] = array(290,287,283,289,281,291,286,280,284,279); //the IDs of the excluded terms
return $args;
}
对于那些在“复选框”中显示类别的人,请使用以下代码。
add_filter('acf/fields/taxonomy/wp_list_categories/name=kathgories', 'my_taxonomy_args', 10, 2);
function my_taxonomy_args( $args, $field ){
$args['include'] = array(197,247,245,250,246,248,249,251);//the IDs of the excluded terms
return $args;
}
还可以使用您自己的 ACF 分类字段名称更改/name=kathgories'
。
因此,随着这种变化,我正在回答我的第二个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.