如何基于属性编写条件PHP

如何基于属性编写条件PHP,php,if-statement,woocommerce,Php,If Statement,Woocommerce,我在互联网上到处搜索,寻找在特定页面上有条件地显示WooCommerce产品属性数据的方法 例如:如果属性somethingcool有一个值,并且它位于/?filtering=1和filter\u manufacturer=1648页面上,则显示该值 如果属性位于特定的筛选的页面上,我希望以不同的方式显示属性 例: 基于此筛选页面,显示产品属性“somethingcool” <?php if ( is_product_attribute('somethingcool') ) {

我在互联网上到处搜索,寻找在特定页面上有条件地显示WooCommerce产品属性数据的方法

例如:如果属性somethingcool有一个值,并且它位于/?filtering=1和filter\u manufacturer=1648页面上,则显示该值

如果属性位于特定的筛选的页面上,我希望以不同的方式显示属性

例:

基于此筛选页面,显示产品属性“somethingcool”

<?php if ( is_product_attribute('somethingcool') ) {
    echo 'Hi! This is something cool?';}
    else {
    echo '';
  }
?>


如果它是一个普通的WordPress页面,而不是一个过滤页面,我可以根据body类标记隐藏和显示,但不幸的是,body类不会根据查询字符串url进行更改。

您可以使用url搜索字符串并从中提取所需的任何部分。例如,如果你只想知道你是否在一个过滤过的页面上,那么你就不会费心去检查制造商

   <?php if ( $product->get_attribute('Certainteed') && intval($_GET['filtered']) == 1 && intval($_GET['filter_manufacturer']) == 1648 ) {
         echo 'Hi! This is something cool?';
         }
   ?>
查看
echo是什么产品属性(“Everlast”)根据您的示例返回

$\u GET…
通过名称获取搜索字符串中的变量,名称放在方括号中

      <?php if( stristr( htmlspecialchars($_SERVER['REQUEST_URI']), 'color-match-tool') && intval($_GET['filtering']) == 1 && intval($_GET['filter_manufacturer']) == 1694 && is_product_attribute('Everlast') ){
             echo 'Hi! This is something cool!';  
             } ?>
然后把这一小部分传给它

 <?php attribute_i_want( intval($_GET['filtering']), 1, intval($_GET['filter_manufacturer']), 1648, $product, $coll, 'Certainteed'); ?>
这将获得从select下拉列表中选择的内容的文本,该文本需要ID“manufacturerName”,并将该文本值插入隐藏输入的值中,PHP将能够在PHP函数调用中选择并使用该值

onchange事件将触发JavaScript并提交页面,php将从隐藏的输入中提取文本值,这就是将放入函数调用的内容:

  <input type="hidden" id ="submittedManufacturerName" name="submittedManufacturerName" value = "" />


除非有任何其他方法可以在PHP变量中获取制造商名称的值,这可能是因为制造商名称确实出现在页面下拉列表上方链接的其他位置,因此它可能已经作为PHP变量存在,您可以按原样使用。这样,您的函数将是完全自动的,而不需要额外的JavaScript。该值出现在现有页面上名为“删除筛选器”的
a href


$manufacturerName
将是从下拉列表中收集的实际值中提交的值,作为
$manufacturerName=htmlspecialchars($\u GET['manufacturerDropdown'])

Steve将其连接起来,并帮助我了解如何编写基于过滤器的条件,从而解决了我的问题。谢谢史蒂夫

<?php if( intval($_GET['filtering']) == 1 && intval($_GET['filter_manufacturer']) == 1648 && $collection == $product->get_attribute('Certainteed') ){ echo '<div class="attribute-titles">Certainteed name: '; echo ($collection == $product->get_attribute('Certainteed') ) ? $collection : __('', 'woocommerce'); echo '</div>'; } ?> 


尝试此操作后,页面中断
尝试此操作后,未呈现任何内容。我的语法正确吗?_product_属性是允许的标记吗?我甚至不能让过滤部分工作。有什么想法吗?这确实奏效了。我最后做了这样的事嗨,汤姆,干得好!不久你将成为一名PHP高手!你也应该把它作为一个答案,因为它有有用的附加位。谢谢Steve,我在PHP方面没有那么高的水平,虽然我不确定is_product_attribute()是否是你自己的函数,但这个函数似乎确实有能力显示产品属性-在你的子主题中将函数添加到你的functions.PHP中。它显示了什么——如果有的话?你能试试
看看你得到了什么值吗?如果有的话?嗨,汤姆,希望刚刚发布的函数对你有用@Tom为函数调用添加了完全自动化的可能想法。Is
Is_product_属性('
伪代码还是真的?我找不到任何对它的引用。如果它不存在,你可以制作一个小函数并将其放入子主题的
functions.php
中,例如,Is_product_属性('Certaineed'))`
return$product->get_attribute('Certainteed');
这只是我编的一个例子。我没有任何自定义函数来支持这一点。如果我不想知道提供的函数是否有任何作用,那就太好了。如果你想要任何帮助来实现它,请告诉我。
    <script language=JavaScript>
    function setManufacturer(){
    var manufacturerName = manufacturerDropdown.options[manufacturerName.selectedIndex].innerHTML;
    documentGetElementById('submittedManufacturerName').value = manufacturerName; 
    }
    </script>
  <input type="hidden" id ="submittedManufacturerName" name="submittedManufacturerName" value = "" />
 <?php attribute_i_want( intval($_GET['filtering']), 1, intval($_GET['filter_manufacturer']), $manufacturerName, $product, $coll, $submittedManufacturerName); ?>
<?php if( intval($_GET['filtering']) == 1 && intval($_GET['filter_manufacturer']) == 1648 && $collection == $product->get_attribute('Certainteed') ){ echo '<div class="attribute-titles">Certainteed name: '; echo ($collection == $product->get_attribute('Certainteed') ) ? $collection : __('', 'woocommerce'); echo '</div>'; } ?>