Matt
Matt

Reputation: 7

Show Advanced Custom Fields repeater child field only when it exists in the post

I am using a repeater field to show products on a page. Some of these require a PDF download link and some don't. I need to be able to show the "Download PDF" link only when a PDF has been uploaded to the file upload repeater child field.

Here's what I'm working with so far:

<?php 
            $rows = get_field('artwork_example');
            if($rows) {
                foreach($rows as $row) {
                    echo '<div class="artwork-content"><img src="', $row['image'];
                    echo '"><div class="right"><h2>', $row['title'];
                    echo '</h2><p>', $row['text'];
                    echo '</p>';
                    if ( get_sub_field('upload_pdf') ) echo '<p><a class="download" href="', $row['upload_pdf'];
                    if ( get_sub_field('upload_pdf') ) echo '">Download PDF</a></p></div></div>'; 
        } 
    }
?>

Any help appreciated, thanks!

Upvotes: 0

Views: 2075

Answers (2)

AlexP
AlexP

Reputation: 127

Use this structure with loop

if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

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

<?php while(has_sub_field('photo_repeater')): ?>

<?php if(get_sub_field('photo_slider')): ?>

<?php while( has_sub_field('photo_slider') ): ?> 

        <?php 
        $img_url = get_sub_field('photo_block');
        $image = aq_resize( $img_url, 200, 200, true );
        ?>
        <img class="image_news" src="<?php echo $image; ?>"  alt="111" />
<?php endwhile; ?>     

<?php endif; ?>

<?php endwhile; ?>   

<?php endif; ?>

<?php endwhile; ?> 

PS - to test sub field inside repeater - repeater

if(get_sub_field('photo_block'))(....)

Upvotes: 1

SBD
SBD

Reputation: 446

You can simply use:

<?php if ($row['upload_pdf'])
{
  //show link
}
else
{
  //hide
}

EDIT:

I would do it like this:

<?php 
            $rows = get_field('artwork_example');
            if($rows) {
                foreach($rows as $row) {
                    echo '<div class="artwork-content"><img src="', $row['image'];
                    echo '"><div class="right"><h2>', $row['title'];
                    echo '</h2><p>', $row['text'];
                    echo '</p>';

                    if ( $row['upload_pdf'])
                    {
                        <a class="download" href="<?php echo $row['url']; ?>">Download PDF</a> <?php
                    }
                    else
                    {
                        echo '';
                    }
        } 
    }
?>

Also, if you are not certain how to get certain values out of ACF, use var_dump.

var_dump($row);

Good luck!

Upvotes: 0

Related Questions