【问题标题】:Load Content on Dropdown Select在下拉选择中加载内容
【发布时间】:2017-12-20 20:14:24
【问题描述】:

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

我通过将每个循环放在一个选项卡中,对选项卡做了类似的事情。我正在尝试以这种方式构建它,因为我希望从长远来看它会减少加载时间并提高效率

【问题讨论】:

  • 您好,欢迎来到 SO。你为什么说它目前不起作用?请多描述一下问题。
  • 当我选择一个下拉列表时,它会自动提交。页面刷新,域保持不变,但它说找不到页面。控制台中没有错误。如果我删除第一个 if 语句,第一个循环将显示,但不会显示 if 语句。
  • 没有缓存插件。

标签: php wordpress forms


【解决方案1】:

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

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

设置“条款” => $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',

【讨论】:

  • 谢谢。当我选择一个下拉列表时,它会自动提交我想要的。页面刷新,域保持不变,但它说找不到页面。控制台中没有错误。如果我删除第一个 if 语句,第一个循环将显示,但不会显示 if 语句。我开始阅读有关管理帖子逻辑的内容,但我还需要做更多。
猜你喜欢
  • 2021-04-19
  • 2015-09-30
  • 2013-03-06
  • 2017-05-06
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
相关资源
最近更新 更多