wordpress 用户配置文件中无法访问自定义分类

8yoxcaq7  于 2023-01-12  发布在  WordPress
关注(0)|答案(2)|浏览(148)

bounty将在6天后过期。回答此问题可获得+100声望奖励。PavKR希望引起更多人关注此问题。

我目前使用高级自定义字段来显示用户配置文件上的复选框字段,这些复选框的label:value是使用ACF的acf/load_field挂钩自动填充的。
钩子可以自动填充每种帖子类型的分类,也可以在ACF组编辑器页面上填充,但是自定义分类在用户配置文件页面上根本不可用。

function acf_load_types($field){
    $user = wp_get_current_user();
    $isAdmin = false;

    if(isset($user->caps['administrator']) && $user->caps['administrator'] == 1){
      $isAdmin = true;
    }

    if($isAdmin){
      $field['choices'] = array();
      
      $types = get_terms(array('taxonomy' => 'user_type', 'hide_empty' => false));
      
      print_r($types); // <--
      /*
        correctly displays the user types on all
        post types and on the ACF Group/Field edit
        pages but shows "Invalid Taxonomy" on the
        user profile page.
      */

      print_r(get_taxonomies()); // <--
      /*
        correctly displays all custom taxonomies on
        all post types but only shows native
        taxonomies (eg. category, post_tag, etc) on
        the user profile page.
      */
      

      if(is_array($types)){
        foreach($types as $type){
          $field['choices'][$type->term_id] = $type->name;
        }
      }
    }
    
    return $field;
  }
  add_filter('acf/load_field/key=field_633df7484d390', 'acf_load_types');

为了注册自定义分类,我使用了以下代码;

$args = [
    'label'                 => 'Types',
    'labels'                => [
                                'name'          => 'Types',
                                'singular_name' => 'Type',
                            ],
    'public'                => true,
    'publicly_queryable'    => true,
    'hierarchical'          => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'show_in_nav_menus'     => false,
    'query_var'             => false,
    'rewrite'               => [
                                'slug'        => 'type',
                                'with_front'  => false,
                            ],
    'show_admin_column'     => true,
    'show_in_quick_edit'    => true,
  ];

  register_taxonomy('user_type', 'user', $args);

是否需要使用其他方法来使user_type自定义分类在用户配置文件编辑页面上可用?

0g0grzrc

0g0grzrc1#

为了使user_type自定义分类在用户配置文件编辑页面上可用,需要将其与"user"帖子类型链接,而不是尝试将其直接添加到用户配置文件页面。
你可以使用register_taxonomy_for_object_type函数来完成这个操作,在你的functions.php文件中,你可以使用下面的代码:

register_taxonomy_for_object_type('user_type', 'user');

这将把"user_type"自定义分类与"user"帖子类型链接起来,并且它应该在用户配置文件编辑页面上可用。如果是这种情况,您也可以在后端检查,看看它是否显示在用户配置文件编辑页面的分类部分下。
您可能需要将$args中的'query_var'=〉false改为'query_var'=〉'user_type',这样您就可以使用get_query_var函数来获取category.php页面上的值。
祝你好运!

5cg8jx4n

5cg8jx4n2#

以下是您可能需要检查和确认的一些事项:
注册自定义分类时,请将“user”参数添加到“object_type”参数。这将使自定义分类可供用户使用:

register_taxonomy('user_type', array('user', 'post_type'), $args);

在您正在使用的函数acf_load_types()中,尝试在调用get_terms()函数时使用“user”而不是“taxonomy”参数:

$types = get_terms(array('object_type' => 'user', 'hide_empty' => false));

还要检查访问页面的用户的能力,管理员用户具有访问所有分类和自定义分类的能力。

相关问题