WooCommerce conditional tags not working

时间:2018-02-03 09:27:28

标签: php wordpress if-statement woocommerce

I am trying to use a mixture of Wordpress and Wooommerce conditional tags to load content on certain pages within my site. However, the WooCommerce conditional tags do not seem to have any effect. In the below example, I am trying to NOT load some code if the user is logged in, or if they are on any WooCommerce page. It works if logged in, but does not take effect on WooCommerce pages?

I have also tried !is_checkout() and !is_account_page(), and neither take effect either?

<?php
// Do not display code if user is logged in OR on any woocommerce pages
if ( !is_user_logged_in() || !is_woocommerce() ) {
    // CODE HERE
} ?>

1 个答案:

答案 0 :(得分:0)

经过一些调查(我还不确定这里的逻辑),结果发现我们需要用AND ||语句替换OR &&语句。

具有OR !的多个||将始终等于true,因为该字符串变量只会等于一个而不是另一个。

这是经过测试和运作的,但任何人都可以了解逻辑吗?我曾假设只有当所有条件都为TRUE时,用||替换所有&&都不会导致执行语句?

以下是更新且正常运行的代码:

替换所有||与&amp;&amp;只有当所有条件都为TRUE时,才会导致在括号中执行语句?

以下是更新且正常运行的代码:

<?php
// Do not display code if user is logged in OR on any woocommerce pages
if ( !is_user_logged_in() && !is_woocommerce() ) {
    // CODE HERE
} ?>