我试图插入一个快捷码作为菜单中的最后一项,但它会导致创建<li>
但随后将短代码输出扔到列表之外的其他地方。它还删除了关闭<li>
对于那个菜单项?
在函数中。php(根据Jared下面的回答)。。
<?php
// Add Cart Contents to End of Nav
add_filter(\'wp_nav_menu_items\',\'my_item\', 11, 2);
function my_item($items, $args) {
$items .= \'<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-last" id="menu-item-last">\' . do_shortcode(\'[shopping_cart empty_msg="You have no items in your basket"]\') . \'</li>\';
return $items;
}
?>
输出为:
<div xmlns="http://www.w3.org/1999/xhtml" class="wrap"><div id="title-area"><p id="title"><a title="asdf" href="asdf">asdfy</a></p><p id="description">asdf</p></div><!-- end #title-area -->
You have no items in your basket
<div id="nav"><div class="wrap"><ul class="nav" id="menu-main-navigation-menu"><li class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-11 current_page_item menu-item-91" id="menu-item-91"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-29" id="menu-item-29"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2838" id="menu-item-2838"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-34" id="menu-item-34"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-31" id="menu-item-31"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-33" id="menu-item-33"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-112" id="menu-item-112"><a href="asdf">asdf</a></li>
<li id="menu-item-last" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-last"/></ul></div><!-- end .wrap --></div></div>
编辑:Cart66ShortcodeManager。php
/**
* Short code for displaying shopping cart including the number of items in the cart and links to view cart and checkout
*/
public function shoppingCart($attrs) {
$cartPage = get_page_by_path(\'store/cart\');
$checkoutPage = get_page_by_path(\'store/checkout\');
$cart = Cart66Session::get(\'Cart66Cart\');
if(is_object($cart) && $cart->countItems()) {
?>
<div id="Cart66scCartContents" style="float:right; text-align: right;">
<a id="Cart66scCartLink" href=\'<?php echo get_permalink($cartPage->ID) ?>\'>
<span id="Cart66scCartCount"><?php echo $cart->countItems(); ?></span>
<span id="Cart66scCartCountText"><?php echo $cart->countItems() > 1 ? \' items\' : \' item\' ?></span>
<span id="Cart66scCartCountDash">–</span>
<!-- <span id="Cart66scCartPrice"><?php //echo CART66_CURRENCY_SYMBOL .
number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); ?> -->
</span></a>
<a id="Cart66scViewCart" href=\'<?php echo get_permalink($cartPage->ID) ?>\'>View Cart</a>
<span id="Cart66scLinkSeparator"> | </span>
<a id="Cart66scCheckout" href=\'<?php echo get_permalink($checkoutPage->ID) ?>\'>Check out</a>
</div>
<?php
}
else {
$emptyMessage = isset($attrs[\'empty_msg\']) ? $attrs[\'empty_msg\'] : \'Your cart is empty\';
//echo "<p id=\\"Cart66scEmptyMessage\\" style=\\"float:right; text-align: right;\\">$emptyMessage</p>";
echo "$emptyMessage";
}
}
最合适的回答,由SO网友:Jared 整理而成
问题是,您试图将某个内容回显到字符串中,echo
应仅用于输出某些内容,因此只需更改以下行:
$items .= \'<li class="myclass">\' . echo do_shortcode(\'[shopping_cart]\') . \'</li>\';
对此:
$items .= \'<li class="myclass">\' . do_shortcode(\'[shopping_cart]\') . \'</li>\';
Edit: 另一方面,这只是一个PHP问题,此类问题应在StackOverflow.Edit 2: 关于您上次对问题的编辑,短代码函数需要return
数据,非echo
这就是问题的根源。你需要带走你的一切echo
ing并将其放入变量中,然后return
该变量(包含要输出的HTML代码)。
/**
* Short code for displaying shopping cart including the number of items in the cart and links to view cart and checkout
*/
public function shoppingCart( $attrs ) {
$cartPage = get_page_by_path( \'store/cart\' );
$checkoutPage = get_page_by_path( \'store/checkout\' );
$cart = Cart66Session::get( \'Cart66Cart\' );
if( is_object( $cart ) && $cart->countItems() ) {
$buffer = \'<div id="Cart66scCartContents" style="float:right; text-align: right;">\';
$buffer .= \'<a id="Cart66scCartLink" href="\' . get_permalink( $cartPage->ID ) . \'">\';
$buffer .= \'<span id="Cart66scCartCount">\' . $cart->countItems() . \'</span>\';
$buffer .= \'<span id="Cart66scCartCountText">\' . $cart->countItems() > 1 ? \' items\' : \' item\' . \'</span> \';
$buffer .= \'<span id="Cart66scCartCountDash">–</span>\';
$buffer .= \'<!-- <span id="Cart66scCartPrice">\' . CART66_CURRENCY_SYMBOL.number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2).\'</span></a> -->\';
$buffer .= \'<a id="Cart66scViewCart" href="\' . get_permalink( $cartPage->ID ) . \'">View Cart</a>\';
$buffer .= \'<span id="Cart66scLinkSeparator"> | </span>\';
$buffer .= \'<a id="Cart66scCheckout" href="\' . get_permalink( $checkoutPage->ID ) . \'">Check out</a>\';
$buffer .= \'</div>\';
return $buffer;
} else {
$emptyMessage = isset( $attrs[\'empty_msg\'] ) ? $attrs[\'empty_msg\'] : \'Your cart is empty\';
return "<p id=\\"Cart66scEmptyMessage\\" style=\\"float:right; text-align: right;\\">" . $emptyMessage . "</p>";
}
}