是否可以在一个函数中注册多个自定义分类?

时间:2013-10-23 作者:codeview

我正在我的函数中注册7个自定义分类法。php。由于每个函数使用相同的标签和参数,并且附加到相同的自定义Post类型,我想知道是否可以在一个函数中注册所有分类法?下面是使用重复7次的代码<XXX> 作为唯一可更换的部件:

function custom_taxonomy_<XXX> ()  {
$labels = array(
    \'name\'                       => \'<XXX>\',
    \'singular_name\'              => \'<XXX>\',
    \'menu_name\'                  => \'<XXX>s\',
    \'all_items\'                  => \'All <XXX>s\',
    \'parent_item\'                => \'Parent <XXX>\',
    \'parent_item_colon\'          => \'Parent <XXX>:\',
    \'new_item_name\'              => \'New <XXX> Name\',
    \'add_new_item\'               => \'Add New <XXX>\',
    \'edit_item\'                  => \'Edit <XXX>\',
    \'update_item\'                => \'Update <XXX>\',
    \'separate_items_with_commas\' => \'Separate <XXX> with commas\',
    \'search_items\'               => \'Search <XXX>s\',
    \'add_or_remove_items\'        => \'Add or Remove <XXX>s\',
    \'choose_from_most_used\'      => \'Choose from the most used <XXX>s\',
);
$args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
    \'query_var\'                  => true,                       
);
register_taxonomy( \'<XXX>\', \'custom_post_type\', $args );
}
add_action( \'init\', \'custom_taxonomy_<XXX>\', 0 );           

2 个回复
SO网友:Rarst

没有。至少不是WordPress的方式。

您可能会使用helper函数(换言之是普通的PHP领域)来构建标签数组的实例,但这将严重阻碍字符串的可翻译性。

SO网友:Aaron Ransley

我将尝试创建一个包装函数,它可以为所有这些XXX\'s 在运行时。下面是一个粗略的、未经测试的示例:

function build_and_register_custom_taxonomy($taxonomy_name, $taxonomy_menu_name, $priority) {
  $definition = function($taxonomy_name) {
    $labels = array(
      \'name\'                       => $taxonomy_name,
      \'singular_name\'              => $taxonomy_name,
      \'menu_name\'                  => "${taxonomy_menu_name}"
      // etc...
    );
    $args = array(
      \'labels\'                     => $labels,
      \'hierarchical\'               => true,
      \'public\'                     => true,
      \'show_ui\'                    => true,
      \'show_admin_column\'          => true,
      \'show_in_nav_menus\'          => true,
      \'show_tagcloud\'              => true,
      \'query_var\'                  => true,
    );
    register_taxonomy($taxonomy_name, \'custom_post_type\', $args );
  }
  add_action(\'init\', $definition, $priority);
}

build_and_register_custom_taxonomy(\'news_item\', \'News Items\', 0)
您需要在这里完成实现,但想法是在填充整个$labels 具有适当值的数组。

你最终可能会build_and_register_custom_taxonomy 函数接受8个参数,或者仅接受3个或4个参数。

您还可以覆盖$args 通过从父级传递参数来设置数组build_and_register_custom_taxonomy 根据需要发挥作用。

祝你好运

结束

相关推荐

Wordpress custom taxonomy

我正在使用一个名为“product\\u cat”的自定义分类法和一个名为“product”的post类型,我正在尝试显示分类法中的所有父术语、子术语和孙术语。所以我得到了这样一个列表,但我只想在子页面上显示孙子术语。Courses (parent) - Industry (child) -- Industry One (grandchild) -- Industry Two (grandchild) -- Industry Three( grandchild) -