我跟着贾斯汀·塔洛克做的这个极好的教程。
我无法更新配置文件,它不会保存到用户配置文件:
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; ?> 发布于 2019-08-30 22:29:49
我想出来了:
现在起作用了!
https://wordpress.stackexchange.com/questions/346281
复制相似问题