在下拉列表中加载内容选择

时间:2017-12-20 20:14:24

标签: php wordpress forms

此代码目前无效。我正在使用wordpress并尝试根据所选的下拉列表显示内容。更具体地说,内容是基于分类法的不同循环。

<form method="post" action="">
<select name="only_criteria" onchange="submit();">
        <option selected="selected">Please Choose</option>
        <option value="one">One</option>
        <option value="two">Two</option>
        <option value="three">Three</option>
</select>
</form>

<div class="archive-wrap">
 <div class="carousel">
<?php 
 if(isset($_POST['only_criteria']) && $_POST['only_criteria'] == '')  {   
$loop = new WP_Query( array( 
    'post_type' => 'my_custom_post_type',
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
            array(
                'taxonomy' => 'my_taxonomy',
                'terms'    => array( 'tax_one','tax_two','tax_three'),
      ),
    ),
  ) );
  } else if(isset($_POST['only_criteria']) && $_POST['only_criteria'] == 'one')  {   
$loop = new WP_Query( array( 
    'post_type' => 'my_custom_post_type',
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
            array(
                'taxonomy' => 'my_taxonomy',
                'terms'    => array( 'tax_one'),
      ),
    ),
  ) );
  } else if(isset($_POST['only_criteria']) && $_POST['only_criteria'] == 'two')  {   
$loop = new WP_Query( array( 
    'post_type' => 'my_custom_post_type',
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
            array(
                'taxonomy' => 'my_taxonomy',
                'terms'    => array( 'tax_two'),
      ),
    ),
  ) );
  } else if(isset($_POST['only_criteria']) && $_POST['only_criteria'] == 'three')  {   
$loop = new WP_Query( array( 
    'post_type' => 'my_custom_post_type',
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
            array(
                'taxonomy' => 'my_taxonomy',
                'terms'    => array('tax_three'),
      ),
    ),
  ) );
  }
?>

我通过将每个循环放在一个标签中来完成与标签类似的事情。我试图以这种方式构建它,因为我希望从长远来看它会减少加载时间并提高效率

1 个答案:

答案 0 :(得分:0)

除了向我们提供更多关于它为什么不起作用的信息之外,还有一些建议。

首先,您可以使用术语参数中的变量将最后3个循环设置压缩为1:

设置&#34;术语&#34; =&GT; $温度; terms参数不必是数组。 使用此逻辑设置$ temp:

switch ($_post['only_criteria']) {
case "one"
  $temp = 'tax_one';
case "two"
  $temp = 'tax_two';
case "three"
  $temp = 'tax_three';
}

Second, I believe you have to use admin-post logic to handle a form in wp. 
You also don't this, since you only have one array: 
'relation' => 'AND',