我已经设置了我的用户配置文件,以允许自定义分类法following this tutorial.
理想情况下,我希望使用Post屏幕中的默认标记元框,以便用户可以轻松添加新术语并查找常用术语。我目前正在使用一个复选框列表,但这可能会很长,因为我们将有许多分类法。我能用一下post_tags_meta_box()
? 我已经调查过了add_meta_box()
但这不适用于\'user\'
.
我已经设置了我的用户配置文件,以允许自定义分类法following this tutorial.
理想情况下,我希望使用Post屏幕中的默认标记元框,以便用户可以轻松添加新术语并查找常用术语。我目前正在使用一个复选框列表,但这可能会很长,因为我们将有许多分类法。我能用一下post_tags_meta_box()
? 我已经调查过了add_meta_box()
但这不适用于\'user\'
.
这个代码对我有用。它使用“位置”自定义分类法和“建议”javascript。You need to extend it to support multiple term selection.
将自定义字段添加到用户编辑屏幕,并在用户/管理员更新配置文件时存储元数据
// for account owner
add_action(\'show_user_profile\', \'add_custom_user_profile_fields\');
add_action(\'personal_options_update\', \'save_custom_user_profile_fields\');
// for admins
add_action(\'edit_user_profile\', \'add_custom_user_profile_fields\');
add_action(\'edit_user_profile_update\', \'save_custom_user_profile_fields\');
function add_custom_user_profile_fields($user) {
printf(
\'
<h3>%1$s</h3>
<table class="form-table">
<tr>
<th><label for="location">%2$s</label></th>
<td>
<input type="text" name="location" id="location" value="%3$s" class="regular-text" />
<br /><span class="description">%4$s</span>
</td>
</tr>
</table>
\', __(\'Extra Profile Information\', \'locale\'),
__(\'Location\', \'locale\'),
esc_attr(get_user_meta($user->ID, \'location\', true)),
__(\'Start typing location name.\', \'locale\')
);
}
function save_custom_user_profile_fields($user_id) {
if (!current_user_can(\'edit_user\', $user_id))
return FALSE;
$location_name = ( isset($_POST[\'location\']) ) ? $_POST[\'location\'] : \'\';
// use your taxonomy name instead of \'locations\'
$location = get_term_by(\'name\', $location_name, \'locations\');
// human readable value and id
update_user_meta($user_id, \'location\', $location_name);
update_user_meta($user_id, \'location_id\', $location->term_id);
}
Enqueue建议javascript仅用于用户编辑屏幕(假设您在自定义主题中使用此选项)function admin_scripts($hook) {
$screen = get_current_screen();
if (\'user-edit\' == $screen->id) {
wp_enqueue_script(
\'user-edit-tag\',
get_stylesheet_directory_uri() . \'/js/usermeta.js\',
array(\'suggest\'),
\'20140509\',
true
);
}
}
usermeta。js公司jQuery(document).ready(function($) {
// use \'tax=your_taxonomy_name\' instead of \'tax=locations\'
$(\'#location\').suggest(ajaxurl+"?action=ajax-tag-search&tax=locations",{
multiple:false,
multipleSep: ","
});
});
不幸的是,我会说答案是否定的。
您可以从以下位置劫持(复制+粘贴)代码post_categories_meta_box()
并尝试使其适应用户,但您会花费更多时间尝试重新连接所有部件。我很确定post meta框使用ajax来保存和添加新术语,因此您必须劫持一些javascript并对其进行修改。
我认为你最好的选择是继续沿着你现在的道路走下去。您可以添加overflow:scroll;
css属性添加到您的复选框容器中,这样它就不会太长。
几个月前,我为前端反向设计了post meta框,这并不难。但我不建议尝试重用核心中的标记和php函数。相反,如果你陷入困境,可以用它们作为指导。
简要回答,仅列出需要做的事情:
WP使用javascript向元框添加所有魔法(拖放、折叠等)->您需要wp_enqueue_script( \'posts\' );
(或者调用了脚本句柄)
我有一个称为“问题”的自定义帖子类型,每个问题都有一个内容页。我用发行号标记我的所有文章,因此,例如,所有发行15的文章都被标记\'issue-15\'.我有一个内容的自定义帖子模板,我使用以下代码调用同一问题编号内的所有问题帖子。$wp_query = new WP_Query( array( \'post_type\' => \'issue\', \'tag\' => \'issue-14\', \'posts_per_page\' =&g