Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Wordpress的选择输入中设置默认选项?_Php_Wordpress - Fatal编程技术网

Php 如何在Wordpress的选择输入中设置默认选项?

Php 如何在Wordpress的选择输入中设置默认选项?,php,wordpress,Php,Wordpress,我正在管理面板中为用户创建自定义字段,如下所示: function additional_profile_fields( $user ) { $departments = get_terms(['taxonomy' => 'department', 'hide_empty' => false]); $userDepartment = get_the_author_meta( 'department', $user->ID ); $regio

我正在管理面板中为用户创建自定义字段,如下所示:

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');
function附加_profile_字段($user){
$departments=get_terms(['taxonomy'=>department','hide_empty'=>false]);
$userDepartment=获取作者元('department',$user->ID);
$regions=get_terms(['taxonomy'=>'region','hide_empty'=>false]);
$userRegion=获取作者元('region',$user->ID);
$industries=get_terms(['taxonomy'=>industry','hide_empty'=>false]);
$userIndustry=获取作者元('industry',$user->ID);
$companys=get_terms(['taxonomy'=>'company','hide_empty'=>false]);
$userCompany=获取作者元('company',$user->ID);
?>
额外配置文件信息
阿夫德林
如果要显示“请选择一个值”,则在未选择任何选项的情况下,使用此选项。您可以使用以下代码:

只需将其作为select的第一个选项子项,放在foreach代码之前

<option value="" disabled selected>Select your option</option>
选择您的选项

您可以添加一个空白的
作为列表中的第一个元素作为占位符样式文本

如果在其上放置
selected=“selected”
disabled=“disabled”
属性,则默认情况下将选中该属性,用户在更改后将无法选择该属性

完整元素如下所示:

<option value="" selected="selected" disabled="disabled">Choose an option&hellip;</option>
选择一个选项&hellip;
对于您的具体示例:

    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&hellip;</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&hellip;</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&hellip;</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&hellip;</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');
function附加_profile_字段($user){
$departments=get_terms(['taxonomy'=>department','hide_empty'=>false]);
$userDepartment=获取作者元('department',$user->ID);
$regions=get_terms(['taxonomy'=>'region','hide_empty'=>false]);
$userRegion=获取作者元('region',$user->ID);
$industries=get_terms(['taxonomy'=>industry','hide_empty'=>false]);
$userIndustry=获取作者元('industry',$user->ID);
$companys=get_terms(['taxonomy'=>'company','hide_empty'=>false]);
$userCompany=获取作者元('company',$user->ID);
?>
额外配置文件信息
阿夫德林
选择一个选项&hellip;

我想为所有数组设置一个默认选项,不仅是空的,而且还想检查用户以前是否选择了某个选项,当然可以,明白。然后您可以执行类似于上面的操作。这是一个不可选择的值,如果加载DOM时没有“选择”任何选项,则会显示该值