如何向WooCommerce插件添加自定义分类

时间:2013-11-25 作者:user1760110

我试图通过针对Woocommerce中的产品帖子类型,为Woocommerce添加自定义分类法。我使用了以下代码并将其添加到functions.php 我没有收到任何错误消息,但分类法也没有显示在Woocommerce中。你能告诉我怎么做吗?或者我做错了什么?

<?php
// Register Custom Taxonomy
function custom_taxonomy_Item()  {

$labels = array(
    \'name\'                       => \'Items\',
    \'singular_name\'              => \'Item\',
    \'menu_name\'                  => \'Item\',
    \'all_items\'                  => \'All Items\',
    \'parent_item\'                => \'Parent Item\',
    \'parent_item_colon\'          => \'Parent Item:\',
    \'new_item_name\'              => \'New Item Name\',
    \'add_new_item\'               => \'Add New Item\',
    \'edit_item\'                  => \'Edit Item\',
    \'update_item\'                => \'Update Item\',
    \'separate_items_with_commas\' => \'Separate Item with commas\',
    \'search_items\'               => \'Search Items\',
    \'add_or_remove_items\'        => \'Add or remove Items\',
    \'choose_from_most_used\'      => \'Choose from the most used Items\',
);
$args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
);
register_taxonomy( \'item\', \'product\', $args );
}
?>

Update

<?php
add_action( \'init\', \'custom_taxonomy_Item\' );

// Register Custom Taxonomy
function custom_taxonomy_Item()  {

$labels = array(
    \'name\'                       => \'Items\',
    \'singular_name\'              => \'Item\',
    \'menu_name\'                  => \'Item\',
    \'all_items\'                  => \'All Items\',
    \'parent_item\'                => \'Parent Item\',
    \'parent_item_colon\'          => \'Parent Item:\',
    \'new_item_name\'              => \'New Item Name\',
    \'add_new_item\'               => \'Add New Item\',
    \'edit_item\'                  => \'Edit Item\',
    \'update_item\'                => \'Update Item\',
    \'separate_items_with_commas\' => \'Separate Item with commas\',
    \'search_items\'               => \'Search Items\',
    \'add_or_remove_items\'        => \'Add or remove Items\',
    \'choose_from_most_used\'      => \'Choose from the most used Items\',
);
$args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
);
register_taxonomy_for_object_type( \'item\', \'product\', $args );

}
?>

1 个回复
最合适的回答,由SO网友:Nicolai Grossherr 整理而成

您必须做到:

add_action( \'init\', \'custom_taxonomy_Item\' );
因为:

使用init操作调用此函数。在操作之外调用它可能会导致麻烦。

参见codex页register_taxonomy. 除此之外:

较好的be safe than sorry 为自定义帖子类型注册自定义分类时。使用register_taxonomy_for_object_type() 就在连接它们的功能之后。否则,您可能会遇到陷阱,其中在parse_requestpre_get_posts.

所以最好加上:

register_taxonomy_for_object_type( \'item\', \'product\' );
除阅读链接的codex页面外,您还可以查看:

  • Use standard WordPress categories with a CPT


    Edit:

    正如我在评论中所说,这对我很有效,这是Code:

    add_action( \'init\', \'custom_taxonomy_Item\' );
    function custom_taxonomy_Item()  {
    $labels = array(
        \'name\'                       => \'Items\',
        \'singular_name\'              => \'Item\',
        \'menu_name\'                  => \'Item\',
        \'all_items\'                  => \'All Items\',
        \'parent_item\'                => \'Parent Item\',
        \'parent_item_colon\'          => \'Parent Item:\',
        \'new_item_name\'              => \'New Item Name\',
        \'add_new_item\'               => \'Add New Item\',
        \'edit_item\'                  => \'Edit Item\',
        \'update_item\'                => \'Update Item\',
        \'separate_items_with_commas\' => \'Separate Item with commas\',
        \'search_items\'               => \'Search Items\',
        \'add_or_remove_items\'        => \'Add or remove Items\',
        \'choose_from_most_used\'      => \'Choose from the most used Items\',
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
    );
    register_taxonomy( \'item\', \'product\', $args );
    register_taxonomy_for_object_type( \'item\', \'product\' );
    }
    

结束

相关推荐

private functions in plugins

我开发了两个插件,其中一个功能相同(相同的名称,相同的功能)。当试图激活两个插件时,Wordpress会抛出一个错误,因为它不允许我以相同的名称定义函数两次。有没有一种方法可以使这个函数只对插件私有,而不使用面向对象编程,也不简单地重命名函数?我不想使用OOP,因为我首先要学习它。此外,我不想重命名该函数,因为我可能也想在其他插件中使用它,而重命名感觉不太合适。