我有一个包含来自不同专业领域的人的网站,我希望能够使用自定义分类法对他们进行分组。我已经Justin Tadlocks tutorial-code 当我注册了一个分类法时,效果很好,但当我注册了另一个分类法时,它们开始表现得很奇怪。目前,我有“职业”和“办公室”这两个分类法。我有五种不同的职业和20多个办公室。如果我选择一个职业并保存,那么即使我没有检查,也可能会检查另外两个职业。
Here\'s my code.
保存条款时会出现问题吗?是应该由一个功能来管理它们,还是应该像现在这样有单独的功能?
EDIT:400行代码有点多,抱歉
它所做的是注册两个分类法,然后将它们“绑定”到用户页面,而不是锁定到默认情况下的“帖子”,然后乘以2。每个分类法都有自己的功能。我不知道是否是保存字段的任务导致了我的问题,但在保存用户配置文件之前,一切都正常。
下面是保存复选框的函数。
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 = $_POST[\'profession\'] ;
/* Sets the terms (we\'re just using a single term) for the user. */
wp_set_object_terms( $user_id, $term, \'profession\', false);
clean_object_term_cache( $user_id, \'profession\' );
}
add_action( \'personal_options_update\', \'my_save_user_office_terms\' );
add_action( \'edit_user_profile_update\', \'my_save_user_office_terms\' );
下面是我如何将我的条款作为复选框输出到admin:
/* If there are any profession terms, loop through them and display checkboxes. */
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) { ?>
<input type="checkbox" name="profession[]" id="profession-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo esc_attr( $term->slug ); ?>" <?php checked( true, is_object_in_term( $user->ID, \'profession\', $term ) ); ?> /> <label for="profession-<?php echo esc_attr( $term->slug ); ?>"><?php echo $term->name; ?></label> <br />
<?php }
}
Edit 2, added answer.
SO网友:talven
谢天谢地,我找到了解决这个问题的简单方法。当将或术语作为复选框/收音机/任何东西输出时,我们需要更改以下内容:
<?php checked( true, is_object_in_term( $user->ID, \'YOUR-TAX\', $term ) ); ?>
对此:
<?php checked( true, is_object_in_term( $user->ID, \'YOUR-TAX\', $term->term_id ) ); ?>
这是我原来帖子中的函数。YOUR-TAX是您输入分类名称的地方。