如何在wordpress上使用PHP/Javascript if/else条件隐藏类?
我对Wordpress函数中的条件(if/else)有一些问题。 如果购物车上的值如何在wordpress上使用PHP/Javascript if/else条件隐藏类?,javascript,php,wordpress,user-interface,notifications,Javascript,Php,Wordpress,User Interface,Notifications,我对Wordpress函数中的条件(if/else)有一些问题。 如果购物车上的值$xs\u product\u count为0,我需要隐藏此。 我该怎么办 我已经尝试使用PHP本机,但我的网站有一个错误 我的归属代码是: <a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class ="mobile-cart-notif offset-cart-menu"> <span class="xs-item-
$xs\u product\u count
为0,我需要隐藏此
。
我该怎么办
我已经尝试使用PHP本机,但我的网站有一个错误
我的归属代码是:
<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class ="mobile-cart-notif offset-cart-menu">
<span class="xs-item-count highlight xscart">
<?php echo esc_html($xs_product_count); ?>
</span>
<i class="icon icon-bag"></i>
</a>
您可以在span标记之前使用以下条件
<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class ="mobile-cart-notif offset-cart-menu">
<?php
if($xs_product_count != 0 ){
?>
<span class="xs-item-count highlight xscart">
<?php echo esc_html($xs_product_count); ?>
</span>
<?php } ?>
<i class="icon icon-bag"></i>
</a>
或者您可以通过下面的代码隐藏完整的部分
<?php
if($xs_product_count != 0 ){
?>
<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class ="mobile-cart-notif offset-cart-menu">
<span class="xs-item-count highlight xscart">
<?php echo esc_html($xs_product_count); ?>
</span>
<i class="icon icon-bag"></i>
</a>
<?php } ?>
我想试试这个:
<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="mobile-cart-notif offset-cart-menu">
<?php if ((int) $xs_product_count > 0) : ?>
<span class="xs-item-count highlight xscart">
<?php echo esc_html($xs_product_count); ?>
</span>
<?php endif; ?>
<i class="icon icon-bag"></i>
</a>
如果将
(int)
放在中的$xs_product_count
前面,如果语句被称为“一个变量到一个类型”,它会将'0'
字符串更改为整数0
,这让我们能够可靠地与
操作符进行比较。“我已经尝试使用PHP Native,但我的网站有一个错误。” ... 你能告诉我们你尝试了什么,这样我们就不会把你已经做过的事情贴出来;e、 g.将其包装在if
语句中:代码>我尝试了一个子主题函数。也许,因为我是WordPress的新手,担心如果我更改原始的主题函数,因为PHP是松散类型的,所以你可以这样做:
(即,检查它是否真实)当然。但试图鼓励编写人类也能轻松解析的代码。:)嘿,如果。。。endif
这里的语法-在我的书中,这已经使它的可解析性提高了10倍;)每一点都很重要!它起作用了!。但它仅在用户刷新浏览器时显示:(
<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="mobile-cart-notif offset-cart-menu">
<?php if ((int) $xs_product_count > 0) : ?>
<span class="xs-item-count highlight xscart">
<?php echo esc_html($xs_product_count); ?>
</span>
<?php endif; ?>
<i class="icon icon-bag"></i>
</a>