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

问题描述 投票:0回答:2

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

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');

这工作正常,但这总是作为我循环的任何数组的默认第一个选项,但我想提供一个默认选项字段,如占位符,如果用户在该字段的数据库中没有任何内容。我怎样才能做到这一点?

php wordpress
2个回答
1
投票

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

如果您在其上放置selected="selected"disabled="disabled"属性,则默认情况下将选中该属性,并且用户在更改后将无法选择它。

完整元素看起来像这样:

<option value="" selected="selected" disabled="disabled">Choose an option&hellip;</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&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');

0
投票

如果您要显示“请选择一个值”此选项,如果未选择任何选项。您可以使用以下代码:

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

<option value="" disabled selected>Select your option</option>
© www.soinside.com 2019 - 2024. All rights reserved.