如何使用全局类别编码

时间:2011-09-30 作者:user9068

有谁能解释一下如何使用全球分类代码吗。我们必须在哪里编写此代码?

function __add_global_categories( $term_id )
{
    if ( get_current_blog_id() !== BLOG_ID_CURRENT_SITE || ( !$term = get_term( $term_id, \'category\' ) ) )
        return $term_id; // bail

    if ( !$term->parent || ( !$parent = get_term( $term->parent, \'category\' ) ) )
        $parent = null;

    global $wpdb;

    $blogs = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = \'{$wpdb->siteid}\'" );
    foreach ( $blogs as $blog ) {
        $wpdb->set_blog_id( $blog );

        if ( $parent && ( $_parent = get_term_by( \'slug\', $parent->slug, \'category\' ) ) )
            $_parent_ID = $_parent->term_id;
        else
            $_parent_ID = 0;

        wp_insert_term( $term->name, \'category\',  array(
            \'slug\' => $term->slug,
            \'parent\' => $_parent_ID,
            \'description\' => $term->description
        ));
    }

    $wpdb->set_blog_id( BLOG_ID_CURRENT_SITE );
}
add_action( \'created_category\', \'__add_global_categories\' );

1 个回复
SO网友:TheDeadMedic

这实际上是一个片段I wrote a while back, 从那以后,插件的效果更好了——我建议check that out 相反

结束

相关推荐

NGINX Multisite Rewrite rules

我对我的新NGINX 安装如果我像下面这样手动访问图像:incredible.devpreview.co/wp-content/blogs.dir/2/files/2011/05/stockvault-colorful-illustration108811.jpg它出现了,但WordPress正在这里寻找它:incredible.devpreview.co/files/2011/05/stockvault-colorful-illustration108811.jpg以下是我的NGINX配置文件中的内容: