Home > other >  Show aspects based on database availability
Show aspects based on database availability

Time:10-20

Some of our products don't have videos & 360 photos.

Through functions.php We have managed to add buttons for 360 photos & videos to our products pages and now we are wanting to only show these videos if there is data available.

This is the code in features.php which we call in functions.php:

<div class="visual-content-buttons">
        <a class="image-button" href="<?php echo get_post_meta($product->id, 'video', true); ?>" data-featherlight="iframe"><img src="https://leckys.com.au/wp-content/uploads/2021/09/360-green-2.png" alt="360 Photo">Video</a>
        <a class="image-button" href="<?php echo get_post_meta($product->id, '360_photo', true); ?>" data-featherlight="iframe"><img src="https://leckys.com.au/wp-content/uploads/2021/09/360-green-2.png" alt="360 Photo">360 Photo</a>
    </div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

We pull in the data if it is available through id, 'video', true); ?>

We would like to add something in there to make no button display when there is no data available.

Please see this link for an example of what it currently looks like:

With data available: https://leckys.com.au/product/drill-bit-twist-jobber-hss-5-32/

Without data available: https://leckys.com.au/product/socket-swt-twin-w-p-10a-250v/

CodePudding user response:

Since get_post_meta returns an empty string if no video or photo found, you can do a simple if statement to only display if it is !empty (not empty).

<?php
$video_href = get_post_meta($product->id, 'video', true);
$photo_href = get_post_meta($product->id, '360_photo', true);
?>

<div class="visual-content-buttons">
<?php
if (!empty($video_href)) {
?>
<a class="image-button" href="<?php echo $video_href; ?>" data-featherlight="iframe"><img src="https://leckys.com.au/wp-content/uploads/2021/09/360-green-2.png" alt="Video">Video</a>
<?php
}
if (!empty($photo_href)) {
?>
<a class="image-button" href="<?php echo $photo_href; ?>" data-featherlight="iframe"><img src="https://leckys.com.au/wp-content/uploads/2021/09/360-green-2.png" alt="360 Photo">360 Photo</a>
<?php
}
?>
</div>
  • Related