我要求用户购物车的总价出现在标题中,但我只希望在用户购物车中有 1 个或多个项目时才显示它。有谁知道我需要在代码中添加什么才能做到这一点?我尝试了一些我在网上找到的建议,但没有一个奏效。
这是我当前的HTML代码(不起作用):
<?php if ( $cart_contents_count > 0 ) { ?>
<a class="cart-contents" href="http://localhost:8888/devo-wordpress/cart">
<div id=basket>
<span class="tot-price"><?php echo WC()->cart->get_cart_total(); ?></span>
<span class="glyphicons glyphicons-shopping-cart"></span>
</div>
</a> }
<?php
endif; ?>
尝试理解您可以使用的if语句 if 语句,如下所示 if 语句
if(CONDITION){
}
或者像这样
if ($value):
endif;
如果您仅使用 php,请使用第一种情况第二个,如果你想在它之间插入html内容
您的代码包含
<?php if ( $cart_contents_count > 0 ) { ?>
作为开始和
<?php endif; ?>
而这为结束哪个错误。像这样使用
<?php if ( $cart_contents_count > 0 ) : ?>
<a class="cart-contents" href="http://localhost:8888/devo-wordpress/cart">
<div id=basket>
<span class="tot-price"><?php echo WC()->cart->get_cart_total(); ?></span>
<span class="glyphicons glyphicons-shopping-cart"></span>
</div>
</a>
<?php endif; ?>