wordpress模板中的动态选择框不显示

时间:2016-05-03 07:32:28

标签: php wordpress templates

我有一个自定义wordpress模板,并希望显示一个动态选择框,其值来自database.My mysql查询运行良好,获取数据和整个代码在核心php中运行完全正常,但我将代码移动到wordpress模板,它无法显示选择框。请帮助我在哪里出错。在wordpress模板上的代码如下:

  <?php
  /*
  Template Name: ajaxsearch
  */


  get_header(); ?>
  <html>
  <head>
  </head>
  <body>
    <div id="primary">
      <div id="content" role="main">

        <?php while ( have_posts() ) : the_post(); ?>

          <?php get_template_part( 'content', 'page' ); ?>

          <?php

          if (is_user_logged_in()):


            global $wpdb;
          $result = $wpdb->get_results("select hotel_id, hotel_name from hotels", OBJECT );
  //print_r($result);die('here');
          ?>
          <select name="id">
            <?php 

            while ($row = $result->fetch_assoc()) {
              unset($id, $name);
              $id = $row['hotel_id'];
              $name = $row['hotel_name']; 
              ?>
              <option value="<?php echo $id; ?>"><?php echo $name; ?></option>
              <?php                
            }
            ?>
          </select>                                  

          <br/><br/>
          <div id="feedback"></div>
          <br/><br/>


          <?php

          endif;                                         

          ?>





        <?php endwhile; // end of the loop. ?>

      </div><!-- #content -->
    </div><!-- #primary -->
  </body>

  <?php get_footer(); ?>

截图: in local i get correct output

output on wordpress

1 个答案:

答案 0 :(得分:0)

**Here is the corrected code:**

<?php
  /*
  Template Name: ajaxsearch
  */


  get_header(); ?>
  <html>
  <head>
  </head>
  <body>
    <div id="primary">
      <div id="content" role="main">

        <?php while ( have_posts() ) : the_post(); ?>

          <?php get_template_part( 'content', 'page' ); ?>

          <?php

          if (is_user_logged_in()):


            global $wpdb;
          $result = $wpdb->get_results("select hotel_id, hotel_name from hotels", OBJECT );
  //print_r($result);die('here');
          ?>
          <select name="id">
            <?php 

          foreach ($locations as $location){
            echo "<option value='".$location->city_id."'>".$location->city_name."</option>";
        }?>
              </select>


          <br/><br/>
          <div id="feedback"></div>
          <br/><br/>


          <?php

          endif;                                         

          ?>





        <?php endwhile; // end of the loop. ?>

      </div><!-- #content -->
    </div><!-- #primary -->
  </body>

  <?php get_footer(); ?>