Carl Papworth
Carl Papworth

Reputation: 1322

if first advanced custom field is empty, use other within <a>-tag

So I am using Advanced Custom Fields for Wordpress, and I have a repeater sub_field where you either can link a file for download, or choose a page link in a different field for an tag my page. My two fields are named: fil_url page_url

This is my normal markup, which returns one, in this case the uploaded file. php/html:

<a href="<?php the_sub_field('fil_url')):) ?>open</a>

So what I want is an if-statement that says that if fil_url is empty, then display page_url instead. But I'm not sure how to write this. Plz help :)

Upvotes: 0

Views: 349

Answers (2)

Carl Papworth
Carl Papworth

Reputation: 1322

After tinkering with @diggy's post I found this sollution:

<?php
$href = ( get_sub_field( 'fil_url' ) ) ? get_sub_field( 'fil_url' ) : get_sub_field( 'page_url' );
<?>

Upvotes: 0

diggy
diggy

Reputation: 6838

<?php
    $file_url = get_sub_field( 'fil_url' );
    $href = ( ! empty( $file_url ) ) ? get_sub_field( 'fil_url' ) : get_sub_field( 'page_url' );
    echo '<a href="' . $href . '">open</a>';
?>

Upvotes: 1

Related Questions