【问题标题】:ACF filter to exclude all categories from selectACF 过滤器从选择中排除所有类别
【发布时间】:2020-02-13 05:32:58
【问题描述】:

我正在为 WordPress 使用“前端提交专业版”和“ACF(非专业版)插件。 我正在使用这些插件为我的用户制作前端帖子创建者。

我有 200 多个类别,因此我想让我的用户更轻松地选择类别。我将创建多个表单,每个表单都会有几个类别可供用户选择。

现在我使用以下过滤器从表单中排除某些类别。

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 个类别。 我不知道这样做,所以我问是否有人可以提供帮助。

我还希望每个过滤器仅适用于一个表单。我需要一些方法将过滤器链接到正确的表单。(可能通过链接或表单名称)

【问题讨论】:

    标签: php wordpress filter advanced-custom-fields


    【解决方案1】:

    用过滤器解决了。

    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;
    }
    

    【讨论】:

    • 此过滤器与我发布的过滤器具有相同的功能。我希望默认排除所有类别并仅包含少数类别。与上述过滤器相反。
    • 请我更改代码继续使用此代码并给我您的想法。
    • 我认为您的代码仅排除所有类别,并没有看到在我想要的类别之后包含的方法。我尝试了您的代码,但不适用于我。不从表单中排除任何类别。
    【解决方案2】:

    回答我的问题。

    我在上面的过滤器中将“排除”更改为“包含”,它似乎完全符合我的要求。

    它只显示我在表格中给出的类别。

    对于那些在“选择”中显示类别的人,请使用以下代码。

    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;
    }
    

    也改/name=kathgories' 使用您自己的 ACF 分类字段名称。

    所以有了这个变化,我正在回答我的第二个问题。

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 2016-10-16
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多