在产品属性之间添加'x',而不是在最后一个属性后添加'x'



当有多个属性时,我试图在属性之间添加一个x,这是我的代码:

$meta_data = $item->get_formatted_meta_data( '' );
if ( $meta_data ) {

foreach ( $meta_data as $meta_id => $meta ) {
if ( in_array( $meta->key, $hidden_order_itemmeta, true ) ) {
continue;
}
$html .= wp_kses_post( force_balance_tags( $meta->display_value ) ) ;
}
}
break;

我可以把它改成:吗

$html .= return implode( ' x ', wp_kses_post( force_balance_tags( $meta->display_value ) ) );

我不完全确定我是否正确理解你,但也许这个例子和PHP的next((函数的使用可以帮助你。

它循环通过一个数组并打印X,直到最后一个数组元素:

<?php
/** Define the array */
$fruits = [
"Apple",
"Banana",
"Orange",
"Kiwi",
"Cherry"
];

/** Start loop */
foreach ($fruits as $fruit) {

/** Print the fruit and "X" if the array is not at the end */
if(next($fruits)) {
echo "$fruit X ";
}
else {
/** Only print the fruit if the array has ended */
echo $fruit;
}
}

输出:

Apple X Banana X Orange X Kiwi X Cherry

我目前还没有安装WP,但我希望它能有所帮助。

最新更新