首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用户配置文件的自定义分类法表单

用户配置文件的自定义分类法表单
EN

WordPress Development用户
提问于 2019-08-30 16:14:29
回答 1查看 157关注 0票数 1

我跟着贾斯汀·塔洛克做的这个极好的教程

我无法更新配置文件,它不会保存到用户配置文件:

代码语言:javascript
复制
add_action( 'init', 'create_profession_tax' );
$post_type = get_post_type();
function create_profession_tax() {
    register_taxonomy(
        'profession',
        '$post_type',
        array(
            'label' => __( 'Profession' ),
            'rewrite' => array( 'slug' => 'profession' ),
            'hierarchical' => true,
        )
    );
}


function my_update_profession_count( $terms, $taxonomy ) {
    global $wpdb;

    foreach ( (array) $terms as $term ) {

        $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );

        do_action( 'edit_term_taxonomy', $term, $taxonomy );
        $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
        do_action( 'edited_term_taxonomy', $term, $taxonomy );
    }
}

/* Adds the taxonomy page in the admin. */
add_action( 'admin_menu', 'my_add_profession_admin_page' );


function my_add_profession_admin_page() {

    $tax = get_taxonomy( 'profession' );

    add_users_page(
        esc_attr( $tax->labels->menu_name ),
        esc_attr( $tax->labels->menu_name ),
        $tax->cap->manage_terms,
        'edit-tags.php?taxonomy=' . $tax->name
    );
}



/* Create custom columns for the manage profession page. */
add_filter( 'manage_edit-profession_columns', 'my_manage_profession_user_column' );


function my_manage_profession_user_column( $columns ) {

    unset( $columns['posts'] );

    $columns['users'] = __( 'Users' );

    return $columns;
}

/* Customize the output of the custom column on the manage professions page. */
add_action( 'manage_profession_custom_column', 'my_manage_profession_column', 10, 3 );


function my_manage_profession_column( $display, $column, $term_id ) {

    if ( 'users' === $column ) {
        $term = get_term( $term_id, 'profession' );
        echo $term->count;
    }
}

function my_save_user_profession_terms( $user_id ) {

    $tax = get_taxonomy( 'profession' );

    /* Make sure the current user can edit the user and assign terms before proceeding. */
    if ( !current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
        return false;

    $term = esc_attr( $_POST['profession'] );

    /* Sets the terms (we're just using a single term) for the user. */
    wp_set_object_terms( $user_id, array( $term ), 'profession', false);

    clean_object_term_cache( $user_id, 'profession' );
    update_user_meta( $user_id, 'profession', $_POST['profession'] );
}

/* Add section to the edit user page in the admin to select profession. */
add_action( 'show_user_profile', 'my_edit_user_profession_section' );
add_action( 'edit_user_profile', 'my_edit_user_profession_section' );


function my_edit_user_profession_section( $user ) {

$tax = get_taxonomy( 'profession' );

/* Make sure the user can assign terms of the profession taxonomy before proceeding. */
if ( !current_user_can( $tax->cap->assign_terms ) )
    return;

/* Get the terms of the 'profession' taxonomy. */
$terms = get_terms( 'profession', array( 'hide_empty' => false ) ); ?>



name; ?>
    





ID, 'profession', $term ) ); ?> /> name; ?> 
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-08-30 22:29:49

我想出来了:

  • 函数中糟糕的顺序使my_save_user_profession_terms出现在my_edit_user_profession_节之前,并且应该是<>相反的
  • 我在foreach之外调用$term

现在起作用了!

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/346281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档