为作者添加类别[Wordpress]

时间:2015-12-14 10:58:25

标签: php wordpress function metadata

我正在尝试将类别添加到作者。我创建了一个用于添加类别的菜单。作者可以选择自己的类别。创建新的类别部分是成功的。但是当我检查类别并按更新时,该值将显示"数组"在meta_value和我的最后部分"在用户页面上显示类别" 也将显示"数组"在列"用户类别"。下面的代码有问题吗?

/*add user category*/
    ...........add user category...........
    add_action( 'init', 'register_user_taxonomy' );

/*add_user_category_menu*/
    ...........add_user_category_menu...........
    add_action( 'admin_menu', 'add_user_category_menu' );

/*Add user category to user profile page*/
    add_action( 'show_user_profile', 'show_user_category' );
    add_action( 'edit_user_profile', 'show_user_category' );
    function show_user_category( $user ) {

    $assigned_terms = wp_get_object_terms( $user->ID, 'user_category' );
    $assigned_term_ids = array();
    foreach( $assigned_terms as $term ) {
    $assigned_term_ids[] = $term->term_id;
    }

    $user_cats = get_terms( 'user_category', array('hide_empty'=>false) );

    echo "<h3>User Category</h3>";

    foreach( $user_cats as $cat ) { ?>
    <input type="checkbox" id="user-category-<?php echo $cat->term_id ?>" <?php if(in_array( $cat->term_id, $assigned_term_ids )) echo 'checked=checked';?> name="user_category[]"  value="<?php echo $cat->term_id;?>"/> 
    <?php
    echo '<label for="user-category-'.$cat->term_id.'">'.$cat->name.'</label>';
    echo '<br />';
    }
    }

/*Save User Category*/
    add_action( 'personal_options_update', 'save_user_category' );
    add_action( 'edit_user_profile_update', 'save_user_category' );
    function save_user_category( $user_id ) {

    $user_terms = $_POST['user_category'];
    $terms = array_unique( array_map( 'intval', $user_terms ) );
    wp_set_object_terms( $user_id, $terms, 'user_category', false );
    update_user_meta($user_id, 'user_category', $user_terms);

    clean_object_term_cache($user_id, 'user_category');
    }

/*show category on user page*/
    function new_contact_methods( $usercategory ) {
    $usercategory['user_category'] = 'user_category';
    return $usercategory;
    }
    add_filter( 'user_contactmethods', 'new_contact_methods', 10, 1 );

    function new_modify_user_table( $column ) {
    $column['user_category'] = 'user category';
    return $column;
    }
    add_filter( 'manage_users_columns', 'new_modify_user_table' );

    function new_modify_user_table_row( $val, $column_name, $user_id ) {
    $user = get_userdata( $user_id );
    switch ($column_name) {
    case 'user_category' :
        return get_the_author_meta( 'user_category', $user_id );
        break;
        default:
    }
    return $return;
    }
    add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3);

0 个答案:

没有答案