0
votes

This probably sounds unusual (it does to me) but in a wordpress image slideshow (using get attachments), if an image doesn't have a caption, I want to keep the previous caption on display until the next populated caption comes around.

Is this possible?:/

Thanks in advance for any help!

1
Welcome to Stack Overflow! What have you tried? Questions without code snippets usually are very hard to answer. - Alex Wayne
Apologies for not being more useful, I've fixed the problem and posted one solution below! - design function

1 Answers

0
votes

Well it's sorted now and apologies for not showing code previously, here's one solution that worked for me;

<?php endif; ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    $attachments = attachments_get_attachments();
    $total_attachments = count($attachments);
        if( $total_attachments > 0 ) :
        for ($i=0; $i < $total_attachments; $i++) : ?>
            <div><img src="<?php echo $attachments[$i]['location']; ?>" alt="<?php echo $attachments[$i]['caption']; ?>" title="<?php echo $attachments[$i]['title']; ?>">
            <?php if($attachments[$i]['caption']==""){
                 $new_caption=$attachments[$i-1]['caption'];    
            }else{
                 $new_caption=$attachments[$i]['caption'];  
            }
            ?>
            <h2 id="tagline" title="<?php echo $attachments[$i]['title']; ?>"><?php echo $new_caption; ?></h2>
            </div>
            <?php endfor; ?>
            <?php endif; ?>
    <?php endwhile; else: ?>