在创世纪菜单中插入快捷代码?

时间:2012-02-28 作者:iCeR

我试图插入一个快捷码作为菜单中的最后一项,但它会导致创建<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">&ndash;</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";
    }
  }

1 个回复
最合适的回答,由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 这就是问题的根源。你需要带走你的一切echoing并将其放入变量中,然后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">&ndash;</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>";

    }

}​

结束

相关推荐

在哪里上传单独的.php文件中的新的自定义快捷代码?

我在一个名为myShortCodes的文件中编写了我的第一个短代码。php。add\\u短代码是否与短代码本身放在同一个PHP文件中?我在哪里上传它?WordPress如何知道如何查看我的文件?我不必修改\\wp\\u includes\\shortcode。php是我吗?到目前为止,我已经尝试上传我的短代码。php收件人可湿性粉剂包括/我的短代码。php可湿性粉剂内容/主题/神话元素/骨架/myShortCodes。php

在创世纪菜单中插入快捷代码? - 小码农CODE - 行之有效找到问题解决它

在创世纪菜单中插入快捷代码?

时间:2012-02-28 作者:iCeR

我试图插入一个快捷码作为菜单中的最后一项,但它会导致创建<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">&ndash;</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";
    }
  }

1 个回复
最合适的回答,由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 这就是问题的根源。你需要带走你的一切echoing并将其放入变量中,然后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">&ndash;</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>";

    }

}​

相关推荐

如何将Java脚本添加到Custom-Page.php模板?

如何将javascript添加到自定义页面。php模板?如何使从w3schools ajax教程获得的以下javascript在自定义页面上工作。php模板?任何帮助都将不胜感激。工作javascript包含在以下HTML中:<!DOCTYPE html> <html> <style> table,th,td { border : 1px solid black; border-collapse: collapse;&#x