如何使woocommerce产品标签分层?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使woocommerce产品标签分层?相关的知识,希望对你有一定的参考价值。
我想更改woocommerce中的产品标签以显示为分层。我试图寻找一个函数或一个钩子来改变它,但找不到任何。
将不胜感激任何帮助或建议来实现这一目标。
谢谢。
答案
测试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 );
另一答案
找到解决方案,如果有人想要做同样的事情:
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);
}
}
init action with a late priority so other taxonomies are loaded
add_action('init','reregister_taxonomy_pro_tags',9999);
另一答案
感谢上面的回答为我节省了大量的研究,但你应该更换
$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);
另一答案
以下是我为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
动作将再次被调用。
以上是关于如何使woocommerce产品标签分层?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 WooCommerce 产品页面中更改 <H1> 标签