我正在为管理员面板中的用户制作自定义字段,如下所示:
function additional_profile_fields( $user ) {
$departments = get_terms(['taxonomy' => 'department', 'hide_empty' => false]);
$userDepartment = get_the_author_meta( 'department', $user->ID );
$regions = get_terms(['taxonomy' => 'region', 'hide_empty' => false]);
$userRegion = get_the_author_meta( 'region', $user->ID );
$industries = get_terms(['taxonomy' => 'industry', 'hide_empty' => false]);
$userIndustry = get_the_author_meta( 'industry', $user->ID );
$companies = get_terms(['taxonomy' => 'company', 'hide_empty' => false]);
$userCompany = get_the_author_meta( 'company', $user->ID );
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="department">Avdeling</label></th>
<td>
<select id="department" name="department"><?php
foreach ( $departments as $department ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $department->name, selected( $userDepartment, $department->name, false ) );
}
?></select>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="region">Region</label></th>
<td>
<select id="region" name="region"><?php
foreach ( $regions as $region ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $region->name, selected( $userRegion, $region->name, false ) );
}
?></select>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="industry">Bransje</label></th>
<td>
<select id="industry" name="industry"><?php
foreach ( $industries as $industry ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $industry->name, selected( $userIndustry, $industry->name, false ) );
}
?></select>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="company">Selskap</label></th>
<td>
<select id="company" name="company"><?php
foreach ( $companies as $company ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $company->name, selected( $userCompany, $company->name, false ) );
}
?></select>
</td>
</tr>
</table>
<?php
}
add_action('user_new_form', 'additional_profile_fields');
add_action('edit_user_profile', 'tm_additional_profile_fields');
add_action('show_user_profile', 'tm_additional_profile_fields');
这很好用,但这总是作为我循环的任何数组的默认第一个选项,但我想提供一个默认选项字段,如占位符,如果用户在数据库中没有任何内容那个领域。 我怎么能这样做?
答案 0 :(得分:1)
您可以将空白<option>
添加为列表中的第一个元素作为占位符样式文本。
如果您在其上放置selected="selected"
和disabled="disabled"
属性,则默认情况下会选择该属性,并且用户在更改后将无法选择该属性。
完整元素如下所示:
<option value="" selected="selected" disabled="disabled">Choose an option…</option>
对于您的具体示例:
function additional_profile_fields( $user ) {
$departments = get_terms(['taxonomy' => 'department', 'hide_empty' => false]);
$userDepartment = get_the_author_meta( 'department', $user->ID );
$regions = get_terms(['taxonomy' => 'region', 'hide_empty' => false]);
$userRegion = get_the_author_meta( 'region', $user->ID );
$industries = get_terms(['taxonomy' => 'industry', 'hide_empty' => false]);
$userIndustry = get_the_author_meta( 'industry', $user->ID );
$companies = get_terms(['taxonomy' => 'company', 'hide_empty' => false]);
$userCompany = get_the_author_meta( 'company', $user->ID );
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="department">Avdeling</label></th>
<td>
<select id="department" name="department">
<option value="" selected="selected" disabled="disabled">Choose an option…</option>
<?php
foreach ( $departments as $department ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $department->name, selected( $userDepartment, $department->name, false ) );
}
?></select>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="region">Region</label></th>
<td>
<select id="region" name="region">
<option value="" selected="selected" disabled="disabled">Choose an option…</option>
<?php
foreach ( $regions as $region ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $region->name, selected( $userRegion, $region->name, false ) );
}
?></select>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="industry">Bransje</label></th>
<td>
<select id="industry" name="industry">
<option value="" selected="selected" disabled="disabled">Choose an option…</option>
<?php
foreach ( $industries as $industry ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $industry->name, selected( $userIndustry, $industry->name, false ) );
}
?></select>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="company">Selskap</label></th>
<td>
<select id="company" name="company">
<option value="" selected="selected" disabled="disabled">Choose an option…</option>
<?php
foreach ( $companies as $company ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $company->name, selected( $userCompany, $company->name, false ) );
}
?></select>
</td>
</tr>
</table>
<?php
}
add_action('user_new_form', 'additional_profile_fields');
add_action('edit_user_profile', 'tm_additional_profile_fields');
add_action('show_user_profile', 'tm_additional_profile_fields');
答案 1 :(得分:0)
如果您要显示“请选择一个值”此选项,如果未选择任何选项。您可以使用以下代码:
在foreach代码之前,只需将其作为select的第一个选项子项。
<option value="" disabled selected>Select your option</option>