我想更改woocommerce中的产品标签以显示为分层。 我试图寻找一个函数或一个钩子来改变它,但找不到任何。
感谢任何帮助或建议来实现这一目标。
谢谢。
答案 0 :(得分:3)
在woocommerce 3.0.4上测试
function my_woocommerce_taxonomy_args_product_tag( $array ) {
$array['hierarchical'] = true;
return $array;
};
add_filter( 'woocommerce_taxonomy_args_product_tag', 'my_woocommerce_taxonomy_args_product_tag', 10, 1 );
答案 1 :(得分:2)
找到解决方案,如果有人想要这样做,那就是这样:
function reregister_taxonomy_pro_tags() {
# the post types that the taxonomy is registered to
$post_types = array('product');
# set this to the taxonomy name
$tax_name = 'product_tag';
# load the already created taxonomy as array so we can
# pass it back in as $args to register_taxonomy
$tax = (array)get_taxonomy($tax_name);
if ($tax) {
# adjust the hierarchical necessities
$tax['hierarchical'] = true;
$tax['rewrite']['hierarchical'] = true;
# adjust the hierarchical niceties (these could be ignored)
$tax['labels']['parent_item'] = sprintf(__("Parent %s"),
$tax->labels->singular_name);
$tax['labels']['parent_item_colon'] = sprintf(__("Parent %s:"),
$tax->labels->singular_name);
# cast caps to array as expected by register_taxonomy
$tax['capabilities'] = (array)$tax['cap'];
# cast labels to array
$tax['labels'] = (array)$tax['labels'];
# register the taxonomy with our new settings
register_taxonomy($tax_name, array('product'), $tax);
}
}
add_action(' init',' reregister_taxonomy_pro_tags',9999);
答案 2 :(得分:0)
感谢上面的回答为我节省了大量的研究,但你应该替换
$tax['labels']['parent_item'] = sprintf(__("Parent %s"),
$tax->labels->singular_name);
$tax['labels']['parent_item_colon'] = sprintf(__("Parent %s:"),
$tax->labels->singular_name);
到
$tax['labels']->parent_item = sprintf(__("Parent %s"),
$tax['labels']->singular_name);
$tax['labels']->parent_item_colon = sprintf(__("Parent %s:"),
$tax['labels']->singular_name);
答案 3 :(得分:0)
这是我为WooCommerce 3.4.5(和WordPress 4.9.8)所做的方法。这是基于@ user2293554的答案。
<?php
function add_hierarchical_product_tags( $taxonomy, $object_type, $args ) {
if ( $taxonomy == 'product_tag' && $args['hierarchical'] != true ) {
$args['hierarchical'] = true;
$args['rewrite']['hierarchical'] = true;
// Update CMS labels
$args['labels']->parent_item = sprintf(__("Parent %s"), $args['labels']->singular_name);
$args['labels']->parent_item_colon = sprintf(__("Parent %s:"), $args['labels']->singular_name);
// Convert to array for register_taxonomy()
$args['labels'] = (array)$args['labels'];
$args['capabilities'] = (array)$args['cap'];
unset( $args['cap'] ); // remove the cap object
register_taxonomy( $taxonomy, $object_type, $args );
}
}
add_action( 'registered_taxonomy', 'add_hierarchical_product_tags', 10, 3 );
需要args['hierarchical'] != true
条件,因此我们不会陷入循环,因为registered_taxonomy
操作将再次被调用。