简体   繁体   English

functions.php中自定义函数中缺少参数

[英]Missing argument within custom function in functions.php

This is related to a Wordpress website using Woocommerce. 这与使用Woocommerce的Wordpress网站有关。 My server error logs log this error for each IP that visits a certain page: 我的服务器错误日志记录了访问某个页面的每个IP的此错误:

Error: PHP Warning: Missing argument 2 for elixa_woo_hide_in_loop() in "filepath"/functions.php on line 239 错误:PHP警告:第239行上“ filepath” /functions.php中的elixa_woo_hide_in_loop()缺少参数2

Here's the code: 这是代码:

/*Within functions.php*/

add_filter( 'woocommerce_product_is_visible', 'elixa_woo_hide_in_loop', 2 );

function elixa_woo_hide_in_loop( $visible, $id ) {
echo "$id outside of if";
if ( $visible && get_field ( '_elixa_hide_cat', $id ) ) {
    echo "$id in if";
    return false;
    }
return $visible;
}

/*Within website, displayed above each product*/

Warning: Missing argument 2 for elixa_woo_hide_in_loop() in "filepath"/functions.php on line 239

outside of if

Here's the hook referenced above: 这是上面引用的钩子:

http://woocommerce.wp-a2z.org/oik_hook/woocommerce_product_is_visible/ http://woocommerce.wp-a2z.org/oik_hook/woocommerce_product_is_visible/

The code above effects a Woocommerce "Browse all products" page where taxonomy-product_cat.php displays all products by category. 上面的代码实现了Woocommerce的“浏览所有产品”页面,其中taxonomy-product_cat.php按类别显示所有产品。

This code was custom written by a developer before me, and is not part of Wordpress or Woocommerce framework. 此代码是我之前的开发人员自定义编写的,并且不是Wordpress或Woocommerce框架的一部分。

I cannot for the life of me understand what this is supposed to do for the loop. 我一生无法理解这对于循环应该做什么。 Strangely enough, when I remove the add_filter, it has zero effect on the product display and the errors disappear. 奇怪的是,当我删除add_filter时,它对产品显示的影响为零,错误消失了。

When tested $id = NULL or 0 to all these queries and I cannot find where this generic variable is being initially declared: 经过测试,对于所有这些查询,$ id = NULL或0时,我无法找到该通用变量最初在何处声明的:

print gettype($id); 打印gettype($ id);
print get_object_vars($id); 打印get_object_vars($ id);
print is_array($id); 打印is_array($ id);
print is_object($id); 打印is_object($ id);
print_r($id); 的print_r($ ID);
print count($id); 打印计数($ id);
print get_class($id); 打印get_class($ id);
print isset($id); 打印isset($ id);
print get_parent_class($id); 打印get_parent_class($ id);
print gettype($id->container); 打印gettype($ id-> container);

Just want to reach out to the community and see if they have seen something similar to this. 只是想联系社区,看看他们是否看到过类似的东西。 Is this a sanitizing function? 这是消毒功能吗? What does it do? 它有什么作用?

You have a mistake when registering the filter. 注册过滤器时出错。 The third parameter is priority, not number of arguments. 第三个参数是优先级,而不是参数数量。 See https://developer.wordpress.org/reference/functions/add_filter/ 参见https://developer.wordpress.org/reference/functions/add_filter/

Change your code to: 将您的代码更改为:

add_filter( 'woocommerce_product_is_visible', 'elixa_woo_hide_in_loop', 10, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM