首页 > 解决方案 > 根据数据可用性显示方面

问题描述

我们的一些产品没有尺寸。

通过functions.php,我们已经设法在我们的产品页面上添加了一个包含高度、深度和宽度的特定维度部分,现在我们只想在有可用数据的情况下显示这些维度。有些产品只有高度和宽度,有些产品没有尺寸,有些产品有全部三个尺寸。

如果无法调用数据,我们希望删除整个列。例如,如果宽度和深度可用但高度不可用,我们不想显示高度标题或空单元格,但我们仍想显示宽度和深度标题和信息。

这是我们在functions.php中调用的features.php中的代码:

<table class="table">
      <tr>
        <th>Height</th>
        <th>Width</th>
        <th>Depth</th>
      </tr>
      <tr>
        <td><?php echo get_post_meta($product->id, 'height', true); ?></td>
        <td><?php echo get_post_meta($product->id, 'width', true); ?></td>
        <td><?php echo get_post_meta($product->id, 'depth', true); ?></td>
      </tr>
    </table>

如果数据通过 id, 'height', true) 可用,我们会拉入数据;?>

请参阅这些链接以获取其当前外观的示例:

有可用数据:https ://leckys.com.au/product/switch-2g-wp-15a-250v-2/

部分数据可用:https ://leckys.com.au/product/socket-swt-twin-wp-10a-250v/

标签: phpwordpresswoocommerce

解决方案


试试这个代码

 <table class="table">
  <tr>
    <?php if(get_post_meta($product->id, "height", true) ): ?><th>Height</th><?php endif; ?>
    <?php if(get_post_meta($product->id, "width", true) ): ?><th>Width</th><?php endif; ?>
    <?php if(get_post_meta($product->id, "depth", true) ): ?><th>Depth</th><?php endif; ?>
  </tr>
  <tr>
    <?php if(get_post_meta($product->id, "height", true) ): ?><td><?php echo get_post_meta($product->id, 'height', true); ?></td><?php endif; ?>
    <?php if(get_post_meta($product->id, "width", true) ): ?><td><?php echo get_post_meta($product->id, 'width', true); ?></td><?php endif; ?>
    <?php if(get_post_meta($product->id, "depth", true) ): ?><td><?php echo get_post_meta($product->id, 'depth', true); ?></td><?php endif; ?>
  </tr>
</table>

推荐阅读