<br> link 中断代码在 function.php (Polylang) 中不起作用

<br> link break code not working in function.php (Polylang)

这是我的代码更新,我仍然无法使用提供的解决方案添加 link 中断。查看下面的代码。

function gift_sender( $cart_item, $cart_item_key ) {
    $gsender = isset( $cart_item['gsender'] ) ? $cart_item['gsender'] : '';
    $gift_label_string = pll_e('Gift Receiver:');
    printf(
    '<br><div><label>%s</label> <textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
    'prefix-cart-notes',
    $cart_item_key,
    $cart_item_key,
    $gsender
    );
 }
add_action( 'woocommerce_after_cart_item_name', 'gift_sender', 10, 2 );


function gift_receiver( $cart_item, $cart_item_key ) {
    $greceiver = isset( $cart_item['greceiver'] ) ? $cart_item['greceiver'] : '';
    $contact_label_string = pll_e('Contact No:');
    printf(
    '<br><div><label>%s</label> <textarea class="%s" id="rcart_notes_%s" data-cart-id="%s">%s</textarea></div>',
    'prefix-cart-notes',
    $cart_item_key,
    $cart_item_key,
    $greceiver
    );
 }
add_action( 'woocommerce_after_cart_item_name', 'gift_receiver', 10, 2 );

如果您使用浏览器在前端检查您的代码,您可以看到以下内容:

Gift Receiver:<div><label></label> <textarea class="prefix-cart-notes" id="cart_notes_cb48bddd1d90be26114366aaaad95806" data-cart-id="cb48bddd1d90be26114366aaaad95806"></textarea></div>

所以你看,字符串不在label标签里面。这就是为什么没有在正确的地方休息的原因。

请尝试将翻译后的字符串保存在变量中,然后将其放入您的 printf 中。

function gift_sender( $cart_item, $cart_item_key ) {
    $gsender = isset( $cart_item['gsender'] ) ? $cart_item['gsender'] : '';
    $cart_label_string = pll_e('Gift Receiver:');
    printf(
    '<br><div><label>%s</label> <textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
    $cart_label_string,
    $cart_item_key,
    $cart_item_key,
    $gsender);
}
add_action( 'woocommerce_after_cart_item_name', 'gift_sender', 10, 2 );

希望对您有所帮助。

我找到了添加 (br) 的解决方案。

function gift_sender( $cart_item, $cart_item_key ) {
    $gsender = isset( $cart_item['gsender'] ) ? $cart_item['gsender'] : '';
    echo '<br/><br/><div><label>';
    pll_e('Gift Receiver:');
    echo '</label>';
    printf(
    '<textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea>',
    'prefix-cart-notes',
    $cart_item_key,
    $cart_item_key,
    $gsender
    );
    echo '</div>';
 }
add_action( 'woocommerce_after_cart_item_name', 'gift_sender', 10, 2 );

function gift_receiver( $cart_item, $cart_item_key ) {
    $greceiver = isset( $cart_item['greceiver'] ) ? $cart_item['greceiver'] : '';
    echo '<div><label>';
    pll_e('Contact No:');
    echo '</label>'; 
    printf(
    '<textarea class="%s" id="rcart_notes_%s" data-cart-id="%s">%s</textarea>',
    'prefix-cart-notes',
    $cart_item_key,
    $cart_item_key,
    $greceiver
    );
    echo '</div>';
 }

add_action( 'woocommerce_after_cart_item_name', 'gift_receiver', 10, 2 );