Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm currently using the advanced custom fields plugin on a wordpress site that I'm developing and it is proving very useful.

I bought the repeater addon which had helped me generate a list of staff members. However I imagine that there won't always be an image available for the staff member so I would like to use a fallback image or use an online placeholder image service like http://placehold.it?

Here is the code:

                <?php if(get_field('about_the_practioner')): ?>

                <?php while(the_repeater_field('about_the_practioner')): ?>

                <article class="colleague_excerpts">

                        <figure>
                            <img src="<?php the_sub_field('image'); ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
                        </figure>

                        <div class="description">
                            <header>
                                <h4><?php the_sub_field('header')?><span><?php the_sub_field('title')?></span></h4>
                            </header>
                            <p><?php the_sub_field('text') ?></p>
                            <a href="<?php the_sub_field('page_link')?>" class="button"><?php the_sub_field('page_link_text') ?></a>
                        </div>
                    <hr>

                </article>

                <?php endwhile; ?>

                <?php endif; ?>

I've seen conditional statements used to create fallback images if a featured image is not available. I'm guessing something similar would be appropriate in this case at the point where the image is called although I'm unsure of the syntax as I'm not good with php. Any help would be appreciated.

share|improve this question
    
You need to check if the the_sub_field exists , this is a PHP question. –  Wyck Mar 15 '13 at 15:37
    
Ok thanks I will try there –  Jonathan Beech Mar 15 '13 at 18:25
add comment

1 Answer 1

Here is the code I used to finally solve this problem a simple if else statement. Hope this may help someone.

<?php if(get_field('about_the_practioner')): ?>

                <?php while(the_repeater_field('about_the_practioner')): ?>

                <article class="colleague_excerpts">

                        <figure>
                            <?php if(get_sub_field('image')): ?>
                            <img src="<?php the_sub_field('image' ) ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
                            <?php else: ?>
                            <img src="http://placehold.it/160x160&text=Awaiting Image">
                            <?php endif; ?> 
                        </figure>

                        <div class="description">
                            <header>
                                <h4><?php the_sub_field('header')?><span><?php the_sub_field('title')?></span></h4>
                            </header>
                            <p><?php the_sub_field('text') ?></p>
                            <a href="<?php the_sub_field('page_link')?>" class="button"><?php the_sub_field('page_link_text') ?></a>
                        </div>
                    <hr>

                </article>

                <?php endwhile; ?>

                <?php endif; ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.