I know the PHP part of this code is wrong but I can’t figure out how to write it correctly. Can someone give me a hand?
function add_last_nav_item($items) { return $items .= '<li><a class="cart-button" href="/cart">View cart (<?php echo WC()->cart->get_cart_contents_count(); ?>)</a></li>'; } add_filter('wp_nav_menu_items','add_last_nav_item');
Advertisement
Answer
No need to <?php echo
from PHP, concatenate the value you need (WC()->cart->get_cart_contents_count()
). Change to:
function add_last_nav_item($items) { return $items .= '<li><a class="cart-button" href="/cart">View cart ('. WC()->cart->get_cart_contents_count() .')</a></li>'; } add_filter('wp_nav_menu_items','add_last_nav_item');