如何使woocommerce产品标签分层?

问题描述 投票:3回答:4

我想更改woocommerce中的产品标签以显示为分层。我试图寻找一个函数或一个钩子来改变它,但找不到任何。

将不胜感激任何帮助或建议来实现这一目标。

谢谢。

wordpress tags woocommerce hierarchy taxonomy
4个回答
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 );

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);
    }
}

init action with a late priority so other taxonomies are loaded

add_action('init','reregister_taxonomy_pro_tags',9999);


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);

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动作将再次被调用。

© www.soinside.com 2019 - 2024. All rights reserved.