php - 根据数据可用性显示方面
问题描述
我们的一些产品没有尺寸。
通过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/
解决方案
试试这个代码
<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>
推荐阅读
- java - Spring Data JPA @Query Annotation 出现问题(SQL 语法错误)
- jvm - 如何使用 Micrometer 和 Alertmanager 在 Prometheus 中提醒 JVM 内存使用情况
- javascript - 在 IE 和 Edge 上不记得 Cookie
- excel - 调整大小和移动图表源范围 VBA
- deck.gl - React 中带有控制器按钮的基本 DeckGL 地图
- sql - Oracle SQL:将多列转置为行
- java - Spliterator Java 8 - 自定义实现
- android - CellDecorator 不检测以编程方式选择的单元格
- cheerio - 我可以在cheerio中获取HTML元素的CSS属性值吗
- react-native - 无法通过 navigationOptions 将 props 传递给组件?