首页 > 解决方案 > 在一些php的输出中添加一个id

问题描述

我有一个小代码,

我需要在输出中添加一个类

$output_html .= ' ' . groovy_menu_woocommerce_mini_cart_counter( $qty ) . ' ';

输出一个

<span class="gm-cart-counter">2</span>

如何在输出中添加 id

' . groovy_menu_woocommerce_mini_cart_counter( $qty ) . '

所以看起来像这样

<span id="newid"class="gm-cart-counter">2</span>

新代码测试

$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'" ;

该工作并显示带有-id id的跨度

但这不是

$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'"  . $span;

标签: phphtmlwordpress

解决方案


这很丑陋,但它应该可以解决问题:

$span = groovy_menu_woocommerce_mini_cart_counter($qty);
$spanWithId = "<span id='the-id'" . substr($span, 5, strlen($span));
$output_html .= $spanWithId;

echo "$spanWithId";

输出:

<span id='the-id' class="gm-cart-counter">2</span>

尝试这个

    global $woocommerce;

    $qty = 0;
    if ($tks == true) {
        $qty = $woocommerce->cart->get_cart_contents_count();
    }

    $cartIcon = 'fa fa-shopping-cart';

    $span = groovy_menu_woocommerce_mini_cart_counter($qty);
    $spanWithId = "<span id='the-id'" . substr($span, 5, strlen($span));

    $output_html .= '
                <div class="gm-minicart minicartmarie">
                    <a href="' . get_permalink( wc_get_page_id( 'cart' ) ) . '" class="gm-minicart-link minicartmarie">
                        <div class="gm-badge">
                            <i class="gm-icon ' . esc_attr( $cartIcon ) . '"></i>
                            ' . $spanWithId . '
                        </div>
                    </a>
                </div>
                ';

您正确替换了对的调用,groovy_menu_woocommerce_mini_cart_counter($qty)但您;在破坏它的语句中包含了 a 。

此外,您应该代替echoing$spanWithIdecho $output_html


推荐阅读