我将以下代码用于带有自定义字段的wordpress模板:
<?php
// vars
$special_product = get_field('special_product', $term);
if (!get_field('special_product') )
if ( $special_product): ?>
<div class="container_wrap container_wrap_first main_color sidebar_right
template-shop">
<div class="container">
<div class="container-left-sp single-product-main-image alpha">
<a class="lightbox-added lightbox avia_image" href="<?php echo $special_product['image']['url']; ?>" rel="lightbox" title="" rel="product_images[grouped]">
<img src="<?php echo $special_product['image']['url']; ?>" alt="<?php echo $special_product['image']['alt']; ?>" /></a>
<div class="thumbnails">
<a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb1']['url']; ?>" rel="lightbox" title="" rel="product_images[grouped]">
<img width="100" height="100" src="<?php echo $special_product['thumb1']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb1']['alt']; ?>" /></a>
<a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb2']['url']; ?>" rel="lightbox" title="" rel="product_images[grouped]">
<img width="100" height="100" src="<?php echo $special_product['thumb2']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb2']['alt']; ?>" /></a>
<a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb3']['url']; ?>" rel="lightbox" title="" rel="product_images[grouped]">
<img width="100" height="100" src="<?php echo $special_product['thumb3']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb3']['alt']; ?>" /></a>
</div>
</div>
<div class="container-right-sp single-product-summary">
<?php echo $special_product['description']; ?>
<a class="avia-slideshow-button avia-button avia-color-orange avia-multi-slideshow-button" href="<?php echo $special_product['product-link']['url']; ?>">
Zum Produkt</a>
</div>
<div>
</div>
</div> <!--close container -->
</div> <!--close container_wrap -->
<?php endif; ?>
使用代码if(!get_field('special_product'))时,我试图隐藏自定义字段,但自定义字段为空但无效。我怎么藏起来?
最诚挚的问候
克里斯
补充: &special_product的输出(自定义字段为空时)为:
Array([image] => [description] => [product-link] => [thumb1] => [thumb2] => [thumb3] =>)
答案 0 :(得分:2)
运行print_r($ special_product)时,您得到:
Array ( [image] => [description] => [product-link] => [thumb1] => [thumb2] => [thumb3] => )
由于empty字段返回的是一个空数组,而不是一无所有,因此永远不能将其视为空,因此您需要检查数组中的键以查看键值是否为空。
我告诉它检查该数组的图像字段以查看其是否为空。
如果您的产品不会总是有图像,那么可以随时对其进行更改,以检查是否存在始终存在的字段。
这是您的工作代码:
// vars
$special_product = get_field('special_product', $term);
<?php if ( $special_product['image'] ): ?>
<div class="container_wrap container_wrap_first main_color sidebar_right
template-shop">
<div class="container">
<div class="container-left-sp single-product-main-image alpha">
<a class="lightbox-added lightbox avia_image" href="<?php echo $special_product['image']['url']; ?>" rel="lightbox" title="" rel="product_images[grouped]">
<img src="<?php echo $special_product['image']['url']; ?>" alt="<?php echo $special_product['image']['alt']; ?>" /></a>
<div class="thumbnails">
<a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb1']['url']; ?>" rel="lightbox" title="" rel="product_images[grouped]">
<img width="100" height="100" src="<?php echo $special_product['thumb1']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb1']['alt']; ?>" /></a>
<a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb2']['url']; ?>" rel="lightbox" title="" rel="product_images[grouped]">
<img width="100" height="100" src="<?php echo $special_product['thumb2']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb2']['alt']; ?>" /></a>
<a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb3']['url']; ?>" rel="lightbox" title="" rel="product_images[grouped]">
<img width="100" height="100" src="<?php echo $special_product['thumb3']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb3']['alt']; ?>" /></a>
</div>
</div>
<div class="container-right-sp single-product-summary">
<?php echo $special_product['description']; ?>
<a class="avia-slideshow-button avia-button avia-color-orange avia-multi-slideshow-button" href="<?php echo $special_product['product-link']['url']; ?>">
Zum Produkt</a>
</div>
</div> <!--close container -->
</div> <!--close container_wrap -->
<?php endif; ?>