简体   繁体   English

在一些php的输出中添加一个id

[英]Adding an id to the output of some php

i have a small code, 我有一个小代码,

i need to add a class to the output 我需要在输出中添加一个类

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

which output a 输出一个

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

how to add an id to the output of 如何在输出中添加ID

' . groovy_menu_woocommerce_mini_cart_counter( $qty ) . '

so it look like this 所以看起来像这样

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

new code tested 经过测试的新代码

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

that work and show a span with the-id id 该工作并显示具有-id id的跨度

but this doesnt 但这不是

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

This is ugly but it should do the trick: 这很丑陋,但可以解决问题:

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

echo "$spanWithId";

Output: 输出:

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

Try this 尝试这个

    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>
                ';

You correctly replaced the call to groovy_menu_woocommerce_mini_cart_counter($qty) but you included a ; 您已正确替换了对groovy_menu_woocommerce_mini_cart_counter($qty)的调用,但其中包括了一个; in the statement which breaks it. 在破坏它的陈述中。

Also, instead of echo ing $spanWithId you should echo $output_html 此外,而不是echo荷兰国际集团$spanWithId你应该echo $output_html

Here's what I would do. 这就是我要做的。

// create a wrapper function that injects the id
function menu_with_id($qty, $id){
  $span = groovy_menu_woocommerce_mini_cart_counter($qty);
  return str_replace('<span', "<span id='$id'", $span);
}

// then replace it
$output_html .= ' ' . menu_with_id($qty, $id) . ' ';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM