以编程方式在Wordpress多站点中创建类别和子类别

时间:2019-02-20 05:58:35

标签: php wordpress multisite

创建一个自定义的wordpress插件,在该插件中,将根据从第3方API获取的数据创建类别和子类别。为了进行测试,我使用了

  

wp_insert_term

对于单个wordpress网站,它运行良好。但这在wordpress多站点中不起作用。考虑到我正在开始创建wordpress插件。

下面是我在单个wordpress网站中用于创建类别和子类别的代码。

wp_insert_term(
// the name of the category
'Category A', 

// the taxonomy, which in this case if category (don't change)
'category', 

array(

// what to use in the url for term archive
'slug' => 'category-a',  
));
wp_insert_term(

    // the name of the sub-category
    'Sub-category Level 1', 

    // the taxonomy 'category' (don't change)
    'category',

    array(
    // what to use in the url for term archive
    'slug' => 'level-1', 

    // link with main category. In the case, become a child of the "Category A" parent  
    'parent'=> term_exists( 'Category A', 'category' )['term_id']

    ));
    wp_insert_term(

        // the name of the sub-category
        'Sub-category Level 2', 

        // the taxonomy 'category' (don't change)
        'category',

        array(
        // what to use in the url for term archive
        'slug' => 'level-2', 

        // link with main category. In the case, become a child of the "Category A" parent  
        'parent'=> term_exists( 'Sub-category Level 1', 'category' )['term_id']

        ));
        wp_insert_term(

            // the name of the sub-category
            'Sub-category Level 3', 

            // the taxonomy 'category' (don't change)
            'category',

            array(
            // what to use in the url for term archive
            'slug' => 'level-3', 

            // link with main category. In the case, become a child of the "Category A" parent  
            'parent'=> term_exists( 'Sub-category Level 2', 'category' )['term_id']

            ));

任何帮助将不胜感激。

0 个答案:

没有答案