没有图像时隐藏 DIV 的规则?
Rule to hide DIV if no image?
希望你一切都好。
我已经让下面的代码按预期工作,但是有没有办法 ONLY 显示 div 'listinggallery' 如果返回图像?
目前,如果列表中有图片,效果很好,但如果没有图片,那么我会显示一个空样式的 div。理想情况下,我想创建一个规则说 "IF listingimage 'true' then show 'listinggallery'".
我试过将 'listinggallery' div 放在代码的其他地方,但似乎使我的网站崩溃了,所以希望我能创建一个规则?
亲切的问候,
斯宾塞
<div class="listinggallery">
<?php
//Get the images ids from the post_metadata
$images = acf_photo_gallery('gallery', $post->ID);
//Check if return array has anything in it
if( count($images) ):
//Cool, we got some data so now let's loop over it
foreach($images as $image):
$id = $image['id']; // The attachment id of the media
$full_image_url= $image['full_image_url']; //Full size image url
$full_image_url = acf_photo_gallery_resize_image($full_image_url, 1024, 768); //Resized size to 262px width by 160px height image url
$thumbnail_image_url= $image['thumbnail_image_url']; //Get the thumbnail size image url 150px by 150px
$url= $image['url']; //Goto any link when clicked
$target= $image['target']; //Open normal or new tab
$alt = get_field('photo_gallery_alt', $id); //Get the alt which is a extra field (See below how to add extra fields)
$class = get_field('photo_gallery_class', $id); //Get the class which is a extra field (See below how to add extra fields)
?>
<div class="listingimage">
<div class="thumbnail">
<?php if( !empty($url) ){ ?><a href="<?php echo $url; ?>" <?php echo ($target == 'true' )? 'target="_blank"': ''; ?>><?php } ?>
<a href="<?php echo $full_image_url; ?>" class="fancybox">
<img src="<?php echo $thumbnail_image_url; ?>" alt="<?php echo $title; ?>" title="<?php echo $title; ?>">
</a>
<?php if( !empty($url) ){ ?></a><?php } ?>
</div>
</div>
<?php endforeach; endif; ?>
</div>
如果您将 <div>
的创建移动到决定是否有任何内容要显示的块内...
<?php
$images = acf_photo_gallery('gallery', $post->ID);
//Check if return array has anything in it
if( count($images) ):
// Output start of gallery div
?>
<div class="listinggallery">
<?php
//Cool, we got some data so now let's loop over it
foreach($images as $image):
// rest of code as it currently is
endforeach;
// Close of gallery div
?>
</div>
<?php
endif;
?>
希望你一切都好。
我已经让下面的代码按预期工作,但是有没有办法 ONLY 显示 div 'listinggallery' 如果返回图像?
目前,如果列表中有图片,效果很好,但如果没有图片,那么我会显示一个空样式的 div。理想情况下,我想创建一个规则说 "IF listingimage 'true' then show 'listinggallery'".
我试过将 'listinggallery' div 放在代码的其他地方,但似乎使我的网站崩溃了,所以希望我能创建一个规则?
亲切的问候, 斯宾塞
<div class="listinggallery">
<?php
//Get the images ids from the post_metadata
$images = acf_photo_gallery('gallery', $post->ID);
//Check if return array has anything in it
if( count($images) ):
//Cool, we got some data so now let's loop over it
foreach($images as $image):
$id = $image['id']; // The attachment id of the media
$full_image_url= $image['full_image_url']; //Full size image url
$full_image_url = acf_photo_gallery_resize_image($full_image_url, 1024, 768); //Resized size to 262px width by 160px height image url
$thumbnail_image_url= $image['thumbnail_image_url']; //Get the thumbnail size image url 150px by 150px
$url= $image['url']; //Goto any link when clicked
$target= $image['target']; //Open normal or new tab
$alt = get_field('photo_gallery_alt', $id); //Get the alt which is a extra field (See below how to add extra fields)
$class = get_field('photo_gallery_class', $id); //Get the class which is a extra field (See below how to add extra fields)
?>
<div class="listingimage">
<div class="thumbnail">
<?php if( !empty($url) ){ ?><a href="<?php echo $url; ?>" <?php echo ($target == 'true' )? 'target="_blank"': ''; ?>><?php } ?>
<a href="<?php echo $full_image_url; ?>" class="fancybox">
<img src="<?php echo $thumbnail_image_url; ?>" alt="<?php echo $title; ?>" title="<?php echo $title; ?>">
</a>
<?php if( !empty($url) ){ ?></a><?php } ?>
</div>
</div>
<?php endforeach; endif; ?>
</div>
如果您将 <div>
的创建移动到决定是否有任何内容要显示的块内...
<?php
$images = acf_photo_gallery('gallery', $post->ID);
//Check if return array has anything in it
if( count($images) ):
// Output start of gallery div
?>
<div class="listinggallery">
<?php
//Cool, we got some data so now let's loop over it
foreach($images as $image):
// rest of code as it currently is
endforeach;
// Close of gallery div
?>
</div>
<?php
endif;
?>