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

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

此代码目前无效。我正在使用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'),
      ),
    ),
  ) );
  }
?>

通过将每个循环放在一个选项卡中,我已经完成了类似的选项。我试图以这种方式构建它,因为我希望从长远来看它会减少加载时间并且效率更高

php wordpress forms
1个回答
0
投票

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

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

设置“terms”=> $ temp; 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',
© www.soinside.com 2019 - 2024. All rights reserved.