【问题标题】:How to add an if statement to my WooCommerce search form如何在我的 WooCommerce 搜索表单中添加 if 语句
【发布时间】:2021-09-05 21:53:12
【问题描述】:

使用内置的 WordPress 搜索表单:

<form role="search" method="get" class="search-form">

<?php
  if ( function_exists( 'woocommerce_product_search' ) ) {
    echo woocommerce_product_search( array( 'limit' => 40 ) );
  }
?>

<div class="col-lg-4 col-lg-offset-4">
  <div class="input-group">
    <input type='textbox' maxlength="8" id="item-search" name="s" class="form-control form-inline" value="<?php the_search_query();?>" placeholder="Enter your postcode e.g. SW1Y 4LG"/>
    <span class="input-group-btn">
      <button class="item-search-btn">search</button>
    </span>
  </div>
</div>

</form>

我已经调用它出现在我的主页上(index.php):

<?php get_search_form(); ?>

我想添加一个if语句来显示:

<div id="postcode-no" class="col-lg-4 col-lg-offset-4">
    Unfortunately this is incorrect.
</div>

所以我在 index.php 文件中添加了:

<?php if (input-text=='SW12') { ?>
    <div id="postcode-no" class="col-lg-4 col-lg-offset-4">
        Unfortunately this is incorrect.
    </div>
<?php } else if (input-text=='SW15'){ ?>
    <div id="postcode-no2" class="col-lg-4 col-lg-offset-4">
        Correct.
    </div>
<?php } ?>

但它不起作用。我对 PHP 相当陌生。我做错了什么?

【问题讨论】:

  • 您可以使用 $input-text 作为 var 而不是 input-text 并首先打印 $input-text 以便您知道将输出什么。并使用 else 语句更好地理解
  • 哦,好吧,我明白你的意思了。谢谢你。你对代码有答案吗? @mollie
  • 请查看以下代码
  • 绝对精彩!谢谢@mollie

标签: php html wordpress


【解决方案1】:
Chaneg the var input-text to $input_text than try 
<?php if ($input_text=='SW12'): ?>
<div id="postcode-no" class="col-lg-4 col-lg-offset-4">
    Unfortunately this is incorrect.
</div>
<?php elseif ($input_text=='SW15'): ?>
<div id="postcode-no2" class="col-lg-4 col-lg-offset-4">
    Correct.
</div>
<?php else: ?>
<div id="postcode-no2" class="col-lg-4 col-lg-offset-4">
    Some Else.
</div>
<?php endif; ?>

【讨论】:

    猜你喜欢
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    相关资源
    最近更新 更多