从高级自定义字段选项页面注册自定义分类

时间:2014-01-20 作者:Lorenzo Pirondini

我有一个产品(鞋)网站,我有一些鞋模型注册为分类功能。php文件如下:

function add_custom_taxonomies() {

    register_taxonomy(\'mod-nike\', \'pompitup_product\', array(
    // Hierarchical taxonomy (like categories)
    \'hierarchical\' => false,
    // This array of options controls the labels displayed in the WordPress Admin UI
    \'labels\' => array(
        \'name\' => _x( \'modeles Nike\', \'taxonomy general name\' ),
        \'menu_name\' => __( \'Modeles Nike\' ),
    ),
    ));
}

add_action( \'init\', \'add_custom_taxonomies\', 0 );
这是工作,因为它应该。

我的问题是,我用这种方式创建了一些自定义分类法,但用户应该能够在后端添加更多分类法,并提供一个选项页。

因此,我尝试挂接“acf/save\\u post”,如下所示:

function my_acf_save_post( $post_id ){
//some logic that take have to do with custom post....

    function register_brand_tax(){

    $brand_to_add = get_field_object(\'field_52d6f9449e688\', \'option\');
    //in the variable $brand_to_add I get the field object from the option pages

    if( !empty($brand_to_add[\'value\'])){


        //then I instert the term on the "brand category" (this works when outisde of a function)
        wp_insert_term(
            $brand_to_add[\'value\'], // the term 
            \'marque\' //the tax
        ); 

        register_taxonomy(\'mod-\' . $brand_to_add[\'value\'], \'pompitup_product\', 
           array(
            \'hierarchical\' => false,
            \'labels\' => array(
                   \'name\' => _x( \'modeles \' . $brand_to_add[\'value\'], \'taxonomy general name\' ),
                    \'menu_name\' => __( \'Modeles \' . $brand_to_add[\'value\'] ),
                          ),
             ));            
        }
    }
}           

add_action( \'init\', \'register_brand_tax\', 20 );
add_action(\'acf/save_post\', \'my_acf_save_post\', 1); 
wp\\u insert\\u术语正在工作,但显然register\\u分类法必须通过“init”操作调用,这就是为什么我试图在“acf/save\\u post”中调用它,但迄今为止没有任何运气,甚至在acf之外,是否有任何方法可以让我的用户从管理后端注册新的分类法?

2 个回复
最合适的回答,由SO网友:Lorenzo Pirondini 整理而成

以下是我最终的做法:

首先是选项页面(用户可以从该页面查看所有品牌并添加新品牌)。

加载时,新品牌字段设置为“”(空)。

// Reset new brand field
add_filter(\'acf/load_field/key=field_52d6f9449e688\', \'reset_new_brand\');

 function reset_new_brand( $field )
    {
        // reset choices
        $field[\'value\'] = \'\';


        // Important: return the field
        return $field;
    }
保存选项页时,将创建一个新类别

// run AFTER ACF saves the $_POST[\'fields\'] data
add_action(\'acf/save_post\', \'my_acf_save_option_page\', 20); 

function my_acf_save_option_page( $post_id ){

        $brand_to_add = get_field_object(\'field_52d6f9449e688\', \'option\');

        if( !empty($brand_to_add[\'value\'])){

                wp_insert_term( $brand_to_add[\'value\'], \'marque\' );
                //delete_option(\'marque_children\'); // clear the cache

             }//end option page stuff
}
编辑帖子时,所有品牌都会从类别中加载

    // Load Brand on edit product
    add_filter(\'acf/load_field/key=field_52b2c7c6bc3e5\', \'load_marque_field\');

 function load_marque_field( $field )
    {
        // reset choices
        $field[\'choices\'] = array();

        // get the textarea value from options page without any formatting
            $choices = get_terms(\'marque\', array(\'parent\' => 0, \'hide_empty\' => false, \'orderby\'=> \'name\' ));

            $arr = array();
        // loop through array and add to field \'choices\'            
            foreach( $choices as $tax_term ) {
                $arr[$tax_term->slug] = $tax_term->name;
                }       

            $field[\'choices\'] = $arr;




        // Important: return the field
        return $field;
    }
编辑或创建帖子时,会有一个名为“new model”的高级自定义字段,如果设置了此字段,则在保存/更新帖子时,会在所选品牌上创建一个新的子类别。保存帖子时,我还会检查是否有任何子类别的计数为0,它们将被删除。

// run BEFORE ACF saves the $_POST[\'fields\'] data
add_action(\'acf/save_post\', \'my_acf_save_post\', 1); 

//Register terms from new model field and category form brand
function my_acf_save_post( $post_id ){
    // vars
    $fields = false;

 if(get_post_type( $post_id ) === "pompitup_product"){
    // load from post
    if( isset($_POST[\'fields\']) ){
        $fields = $_POST[\'fields\'];
        $selected_brand = $fields[field_52b2c7c6bc3e5];
        $new_tax_field = $fields[field_52c53c86e1fb2];
        $existing_tax_field = $fields[field_52dbf2af72c54];         

            if( isset($new_tax_field)){

                $new_tax = $new_tax_field;              

                update_field(\'field_52dbf2af72c54\', $new_tax, $post_id);

                //check if the term exist in selected brand
                $term = term_exists($new_tax, $selected_brand);
                if ($term == 0 ) {
                //get selected brand infos (id)
                $getTermOptions = get_term_by(\'slug\', $selected_brand, \'marque\');

                //insert new term in selected brand

                wp_insert_term( $new_tax, \'marque\', array( \'parent\'=> (int)$getTermOptions->term_id ) );
                delete_option(\'marque_children\'); // clear the cache


                }       




            }else{

                if( isset($existing_tax_field)){ $new_tax = $existing_tax_field; }  

            }

            //set category (brand)
            wp_set_object_terms( $post_id, $selected_brand, \'marque\', false );

            //set category model
            wp_set_object_terms( $post_id, $new_tax, \'marque\', true );

            //delet emtpy tax
            $taxonomyName = "marque";

            //This gets top layer terms only.  This is done by setting parent to 0.  
            $parent_terms = get_terms($taxonomyName, array(\'parent\' => 0, \'hide_empty\' => false));   
            foreach ($parent_terms as $pterm) {
                //Get the Child terms
                $terms = get_terms($taxonomyName, array(\'parent\' => $pterm->term_id, \'hide_empty\' => false));
                foreach ($terms as $term) {

                     if( 0 == $term->count ){
                          wp_delete_term( $term->term_id, $term->taxonomy);
                     }  

                }
            }           


        }//end isset($_POST[\'fields\'])---->for post
    }//end check if post-type== pompitup_product


    // ...
}//end my_acf_save_post

SO网友:Tom J Nowell

相反,考虑一个单一的层次分类法,其中顶级术语是Nike/Adidas等品牌,子术语是模型。

否则,您将面临一个扩展问题,随着品牌数量的增加,分类法的数量也会增加,幕后代码的复杂性也会增加。

结束

相关推荐

Order custom taxonomy by date

我有一个名为series的自定义分类法。我想有一个页面,其中列出了所有系列与最新的顶部。系列鱼类系列关于狗的系列文章我创建了一个分类学系列。php,其中列出了各个系列,但我要http://example.com/series. 我有一个404。我是否需要在主题上创建单独的页面?列出所有按日期排列的系列?