probablybest probablybest - 8 days ago 7
PHP Question

if get_field using multiple fields

Im using advanced custom fields and I want to display a line if both fields are active.

I currently have one checking for a spotify link and one checking for an iTunes link. What I would like to do is display a | (to separate the text) if both fields are being used otherwise the | remains hidden.

Here is my code:

<?php if( get_field('spotify') ): ?>
<a target="_blank" href="<?php the_field('spotify');?>">Spotify</a>
<?php endif; ?>

<?php if( get_field('spotify','itunes') ): ?>
|
<?php endif; ?>

<?php if( get_field('itunes') ): ?>
<a target="_blank" href="<?php the_field('itunes');?>">iTunes</a>
<?php endif; ?>

Answer

Why don't you try :

<?php if(get_field('spotify') && get_field('itunes')): ?>

You could also avoid the reuse of the get_field function :

<?php 
$spotify = false;
if( get_field('spotify') ) : 
    $spotify = true; 
?>
    <a target="_blank" href="<?php the_field('spotify');?>">Spotify</a>
<?php endif; ?>

<?php if( get_field('itunes') ): ?>
    <?php if ($spotify) : ?>|<?php endif; ?>
    <a target="_blank" href="<?php the_field('itunes');?>">iTunes</a>
<?php endif; ?>

Edit : you may have a close result using just this CSS :

#mylinksdiv a + a {
    border-left: 1px solid black;
    padding-left: 0.3em;
}

demo

Comments