名称未显示在下拉列表中

时间:2016-10-18 09:54:36

标签: php

我试图在php中填充下拉列表。它没有向我显示下拉列表中的值。但当我看到它的来源时,它正在向我展示。当我看到它的来源时,它会向我展示像这样的价值

<select name="choose" id="chooseme">
  Urban Striped Sleeveless<option value="48514" name=""> </option>Black Leather High Top Sneakers<option value="19264" name=""> </option>Navy Knit Cardigan<option value="19263" name=""> </option>.....

以下是我正在使用的代码..

<?php
      var_dump($_GET["id"]);

      $args = array( 'post_type' => array('product') ,'posts_per_page' => 100);
      $loop = new WP_Query( $args );

      while ( $loop->have_posts() ) : 
          $loop->the_post();
         //echo '<option value="one">one</option>';
            $title = the_title();
            $value =$loop->post->ID;
       echo "<option value='$value'> $title </option>";

      endwhile;
      ?>

7 个答案:

答案 0 :(得分:0)

请理解下拉工作:

<select name="drpDown">
<option value="1">One</option>
<option value="2">Two</option>
</select>

// here value is what we get when we refer it using its name "drpDown" and the text passed between <option> #### </option> is the text that is shown in drop down.

答案 1 :(得分:0)

尝试使用此方法以避免引用

之间的混淆
<?php
          var_dump($_GET["id"]);

          $args = array( 'post_type' => array('product') ,'posts_per_page' => 100);
          $loop = new WP_Query( $args );

          while ( $loop->have_posts() ) : 
              $loop->the_post();
             //echo '<option value="one">one</option>';
                $title = the_title();
                $value =$loop->post->ID;
                ?>
         <option value='<?php echo $value; ?>'> <?php echo $the_title; ?> </option>
    <?php
          endwhile;
          ?>

答案 2 :(得分:0)

我认为你有语法错误!

尝试始终使用变量:

".$variable."

在你的情况下:

echo "<option value='".$value."'>" .$title. "</option>";

答案 3 :(得分:0)

?php
  var_dump($_GET["id"]);

  $args = array( 'post_type' => array('product') ,'posts_per_page' => 100);
  $loop = new WP_Query( $args );

  while ( $loop->have_posts() ) : 
      $loop->the_post();
     //echo '<option value="one">one</option>';
        $title = the_title();
        $value =$loop->post->ID;
   echo "<option value='$value'> ".$title."</option>";

  endwhile;
  ?>

答案 4 :(得分:0)

使用以下提及的代码:               array('product'),'posts_per_page'=&gt; 100);               $ loop = new WP_Query($ args);

          while ( $loop->have_posts() ) : 
              $loop->the_post();
                $title = the_title();
                $value =$loop->post->ID; ?>
           <option value="<?php echo $value; ?>"> <?php echo $title; ?> </option>
    <?php
          endwhile;
          ?>
</html>

答案 5 :(得分:0)

尝试使用以下代码。当您使用将打印值的get_the_title()时,您需要检索the_title()之类的标题值

<?php
      var_dump($_GET["id"]);

      $args = array( 'post_type' => array('product') ,'posts_per_page' => 100);
      $loop = new WP_Query( $args );

      while ( $loop->have_posts() ) : 
          $loop->the_post();
         //echo '<option value="one">one</option>';
            $title = get_the_title(); // this will return the value of title
            $value = $loop->post->ID;
       echo "<option value='$value'> $title </option>";

      endwhile;
      ?>

参考链接

https://developer.wordpress.org/reference/functions/get_the_title/ https://codex.wordpress.org/Function_Reference/the_title

答案 6 :(得分:0)

Just to avoid confusion, please separate the HTML and PHP. Like this:

<?php
      var_dump($_GET["id"]);

      $args = array( 'post_type' => array('product') ,'posts_per_page' => 100);
      $loop = new WP_Query( $args );
      $posts = "";
      while ( $loop->have_posts() ) : 
          $loop->the_post();
          $title = the_title();
          $value =$loop->post->ID;
          $posts .= "<option value='" . $value . "'>" . $title . "</option>";

      endwhile;
?>

<select name="choose" id="chooseme">
    <?php echo $posts; ?>
</select>

希望它有所帮助。