Conditional PHP if and else statements within existing accordion

I am using Advanced Custom Fields plugin in WordPress and have managed (with very limited PHP knowledge) to use their repeater fields to create an accordion.

Everything is working really well except the image field (company_logo). So long as the user selects an image for this custom field it displays fine but if they do not select an image I get some strange text instead.

Read More

Using my current code I’m trying to add in an ‘if’ statement so if they do not select an image it displays a default image instead. I’ve stride lots of variations but cannot get it to work.

Can anyone help/point me in the right direction please? Also, if theres a way I can clean this up as I seem to use a lot of

<div id="accordion">
<?php if( have_rows('exhibitor') ): ?>              
    <?php while( have_rows('exhibitor') ): the_row(); ?>

        <h4 class="accordion-toggle"><?php the_sub_field('exhibitor_type'); ?></h4>
        <div class="accordion-content"> 

    <?php while( have_rows('table') ): the_row(); ?>
        <div class="exhibitor-single">                    
            <p class="table-field">Table <?php the_sub_field('table_no'); ?></p>           
            <p><?php the_sub_field('company_name'); ?></p>  

            <?php $image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo'); ?>
            <img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />

            <p><a href="http://<?php the_sub_field('company_website'); ?>" target="blank"><?php the_sub_field('company_website'); ?></a></p>
        </div>
<?php endwhile; ?>

            </div>
<?php endwhile; ?>
<?php endif; ?>
</div> 

Related posts

Leave a Reply

1 comment

  1. You should be able to just wrap the image ‘section’ in this:

    <?php
    if(get_sub_field('company_logo')) : 
        $image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo');
        ?>
        <img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />
        <?php
    else :
        ?>
        <!-- Your default image here -->
        <img src="" alt="" />
        <?php
    endif; ?>