Я использовал приведенный ниже код для отображения определенного количества атрибутов на настраиваемой вкладке с помощью короткого кода. Это работает нормально, но не все продукты имеют одинаковые характеристики. Как я могу скрыть строки, которые не содержат данных?
Я ввел этот код:
// Display grouped attributes Gewicht en Omvang
function get_product_attributes_gewicht_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes-gewicht' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$gewicht = $product->get_attribute( 'Gewicht (gram)' );
$hoogte = $product->get_attribute( 'Hoogte (mm)');
$lengte = $product->get_attribute( 'Lengte (mm)');
$breedte = $product->get_attribute( 'Breedte (mm)');
return '<div class = "divTableAtt LichtBlauweRegels">' .
'<div class = "divTableAttBody">' .
'<div class = "divTableAttRow">' .
'<div class = "divTableAttCell">Gewicht (gram)</div>' .
'<div class = "divTableAttCell">' . $gewicht . '</div>' .
'</div>' .
'<div class = "divTableAttRow">' .
'<div class = "divTableAttCell">Hoogte (mm)</div>' .
'<div class = "divTableAttCell">' . $hoogte . '</div>' .
'</div>' .
'<div class = "divTableAttRow">' .
'<div class = "divTableAttCell">Lengte (mm)</div>' .
'<div class = "divTableAttCell">' . $lengte . '</div>' .
'</div>' .
'<div class = "divTableAttRow">' .
'<div class = "divTableAttCell">Breedte (mm)</div>' .
'<div class = "divTableAttCell">' . $breedte . '</div>' .
'</div>' .
'</div>' .
'</div>';
}
}
add_shortcode( 'display-attributes-gewicht', 'get_product_attributes_gewicht_shortcode' );`
Я хочу скрыть, если атрибут в этой строке пуст. Я пытался использовать оператор if для этой строки, но он не работает.
if (isset($breedte)){
return '<div class = "divTableAttRow">' .
'<div class = "divTableAttCell">Breedte (mm)</div>' .
'<div class = "divTableAttCell">' . $breedte . '</div>' .
'</div>';
}
Раньше я закрывал строку с помощью ; и начал строку с нового оператора возврата.
В результате отображаются все результаты других функций, а не только этот набор, как предполагалось.
Я все еще изучаю это и выясняю, как заставить это работать.
🤔 А знаете ли вы, что...
PHP поддерживает множество библиотек для работы с RESTful API.
Попробуйте следующий пересмотренный короткий код, который будет отображать только определенные атрибуты продукта:
// Display grouped attributes Gewicht en Omvang
add_shortcode( 'display-product-attributes', 'shortcode_display_product_attributes' );
function shortcode_display_product_attributes( $atts ) {
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-product-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$gewicht = $product->get_attribute('Gewicht (gram)');
$hoogte = $product->get_attribute('Hoogte (mm)');
$lengte = $product->get_attribute('Lengte (mm)');
$breedte = $product->get_attribute('Breedte (mm)');
if ( $gewicht || $hoogte || $lengte || $breedte ) {
$output = '<div class = "divTableAtt LichtBlauweRegels">
<div class = "divTableAttBody">';
if ( $gewicht ) {
$output .= '<div class = "divTableAttRow">
<div class = "divTableAttCell">Gewicht (gram)</div>
<div class = "divTableAttCell">' . $gewicht . '</div>
</div>';
}
if ( $hoogte ) {
$output .= '<div class = "divTableAttRow">
<div class = "divTableAttCell">Hoogte (mm)</div>
<div class = "divTableAttCell">' . $hoogte . '</div>
</div>';
}
if ( $lengte ) {
$output .= '<div class = "divTableAttRow">
<div class = "divTableAttCell">Lengte (mm)</div>
<div class = "divTableAttCell">' . $lengte . '</div>
</div>';
}
if ( $breedte ) {
$output .= '<div class = "divTableAttRow">
<div class = "divTableAttCell">Breedte (mm)</div>
<div class = "divTableAttCell">' . $breedte . '</div>
</div>';
}
return $output . '</div></div>';
}
}
}
Код находится в файле function.php вашей дочерней темы (или в плагине). Это должно сработать.