levystore
levystore

Reputation: 3

How to make php condition "If...else..." works correctly?

My fonction should be very simple but it's not so easy for me, I need help for that.

Here is my website and I use Custom fields so display into a modal box : - either an image, when there is any - or a soundcloud embed track, when there is any Never both! But both shown by a thumbnail on homepage.

This is my code which is working actually but there is some bug like, why is there a souncloud box on the page, it should be on the lightbox.

<div id="post">
        <a href="#" data-featherlight="#fl3">       

<?php
if ( get_field('music') and get_field('music') != '' ) {
?>
<iframe width="100%" height="166" scrolling="no" class="lightbox" class="frame" id="fl3" frameborder="no" src="https://w.soundcloud.com/player/?url=<?php the_field('music'); ?>&amp;color=1b1e25&amp;theme_color=1b1e25&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false"></iframe> 
<?php } else { ?>



                         <div

<?php $image = get_field('image');
if( !empty($image) ): ?>

class="img" 
href="<?php  $image = get_field('image');if( !empty($image) ): ?> <?php echo $image['url']; ?>"
alt="<?php echo $image['alt']; ?> <?php endif; ?>" 
data-featherlight="image" >
                             <?php endif; ?> 

<?php } ?>


    <?php
        if ( has_post_thumbnail() ) {
        the_post_thumbnail('post-thumbnails');
        }
    ?>
            </div>
    </a>
</div>

The idea is simply : If there is a music, get the music. else Get the image

any idea? Sorry if my code is a bit dirty, I'm not a developper.

Upvotes: 0

Views: 64

Answers (1)

Roel
Roel

Reputation: 3096

If you use php code inside of a html 'template' I would recommend the Alternative syntax for control structures.

See: http://php.net/manual/en/control-structures.alternative-syntax.php

Then it will be something like this (it needs to be cleaned though):

<?php $image = get_field('image') ?>
<div id="post">
<?php if ( get_field('music') and get_field('music') != '' ): ?>
<iframe width="100%" height="166" scrolling="no" class="lightbox" class="frame" id="fl3" frameborder="no" src="https://w.soundcloud.com/player/?url=<?php the_field('music'); ?>        &amp;color=1b1e25&amp;theme_color=1b1e25&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false"></iframe> 
<?php elseif( !empty($image) ): ?>
    <a href="<?php echo $image['url']; ?>">
    <img
        class="img"             
        alt="<?php echo $image['alt']; ?> 
        data-featherlight="image" />

    <?php
        if ( has_post_thumbnail() ) {
            the_post_thumbnail('post-thumbnails');
        }
    ?>
     </a>
     </div>
    <?php endif; ?>
</div>

This is probably not totally correct, but there is much wrong with the structure of your HTML. class="img" alt="<?php echo $image['alt']; ?> data-featherlight="image" is not added to a tag. So I assumed it had to be and img-tag. Also there are mixed attributes that belong to an a-tag.

Also the $image variable is defined twice.

Upvotes: 1

Related Questions