Wordpress自动创建分类法

时间:2015-07-20 14:57:03

标签: php wordpress taxonomy custom-fields

我正在尝试构建类似WooCommerce Attributes的系统。

这意味着某些分类法会即时创建......

因此,我注册了一个特定的新分类法:

function create_my_tax() {
    register_taxonomy(
        'product_pictograms',
        'product',
        array(
            'label' => __( 'Pictograms' ),
            'hierarchical' => true,
            'show_ui' => true,
            'query_var' => true,
        )
    );
}
add_action( 'init', 'create_my_tax' );

另一个功能现在从每个product_pictograms术语创建新的分类法:

function create_more_taxes() {

    $terms = get_terms( 'product_pictograms', array( 'hide_empty' => 0 ) );

    if ($terms) { // If there are any terms

        foreach ($terms as $term) {

            $term_slug = $term->slug; // Term slug
            $term_name = $term->name; // Term name

            register_taxonomy(
                'pi_'.$term_slug,
                'product',
                array(
                    'label' => $term_name,
                    'hierarchical' => false,
                    'show_ui' => false,
                )
            );

        }//END foreach $term

    }//END if $terms

}
add_action( 'init', 'create_more_taxes' );

您可以看到这些新分类法的创建方式与'pi_'.$term_slug一样。

因此,当我在product_pictograms中输入新字词时,例如Term1, Term2, Term3,会有3个新的分类法称为pi_term1, pi_term2, pi_term3

到目前为止一切正常。

但我还要为所有pi_*分类法添加一个自定义字段。

我可以为特定分类创建自定义字段,这不是问题,但如何将此字段添加到所有创建的分类中?

在我尝试使用类似内容的那一刻,但它不起作用

function add_all_terms(){

    $terms = get_terms( 'product_pictograms', array( 'hide_empty' => 0, 'parent' => 0 ) );

    foreach ($terms as $term) {
        $string = 'pi_'.$term->slug;

        add_action( $string.'_add_form_fields', 'add_series_image_field', 10, 2 );
        add_action( $string.'_edit_form_fields', 'journey_series_edit_meta_field', 10, 2 );
        add_action( 'edited_'.$string, 'save_series_custom_meta', 10, 2 );  
        add_action( 'create_'.$string, 'save_series_custom_meta', 10, 2 );
    }   
}
add_action( 'after_setup_theme', 'add_all_terms' );

我尝试了这个确切的函数没有的foreach循环,我用$string = 'pi_term1';替换了$ string ...这样可行。

因此我的foreach循环必定存在不正确的内容。 我还尝试使用此代码设置do_action,但只要add_action位于foreach内,似乎没有任何效果。

我无法弄清楚如何编写函数来触发所有add_actions。

0 个答案:

没有答案