dsj60862 2015-06-22 11:57
浏览 73
已采纳

Wordpress结束语句

Apologies, PHP is not my strongest area so this might seem super easy to others.

I am trying to implement a statement to say, when there is something in my WooCommerce Cart to show the cart. If there's nothing in the cart then show nothing.

The code I have so far is:

<?php if ( sizeof( $woocommerce->cart->cart_contents ) == 0 ) {
  // The cart is empty
} else {

<div class="header-cart-inner">
  <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
</div>

} ?>

The code doesn't work and keeps giving me syntax errors.

  • 写回答

2条回答 默认 最新

  • douren2831 2015-06-22 11:59
    关注
    <?php if ( sizeof( $woocommerce->cart->cart_contents ) == 0 ) {
      // The cart is empty
    } else { ?>
    
    <div class="header-cart-inner">
      <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
    </div>
    <?php } ?>
    

    That should do!

    Explanation :
    All PHP code is going between <?php #php code# ?> everything between these two tags is going to be compiled by the PHP engine. You didn't close your PHP after else { because of this your next piece of script <div clas... is going to be interpreted as PHP code. Now the PHP engine is going to read this and doesn't know what to make of it since it's not PHP and will throw an error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等