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.

Read More

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; ?>

Related posts

Leave a Reply

4 comments

  1. 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

  2. You also could make good use of get_fields( $postID ) function. Like so:

    $fields = get_fields( $post->ID );
    
    if( isset( $fields['spotify'], $fields['itunes'] ) ){
      // do something
    } else {
      // do something else
    }
    
  3. Achievable using CSS ONLY

    You can actually achieve this using just CSS. What you can do is make use of the + selector in CSS. If there are two of them, like – let’s say <span>‘s, then you could select the second one using the + selector and add a pseudo :before to it with content: " | ";

    The best thing about this is it’s all on the client side and does not require your server to do multiple get_field()‘s

    There’s only two that you need to pull and that’s too much for ACF a get_fields().

    Another good thing is if you need to add more in the future, you don’t have to do another check like you are doing right now with the current solution.

    See my example bellow:

    span + span:before {
      content: ' | ';
    }
    <span>Spotify</span><span>iTunes</span>
  4. I have created the following snippet in which you can just add your links in the array $fields, and it displays everything in the same manner as you had, but without all the weird conditionals:

    <?php
        $links = '';
        $fields = array(
            'spotify' => 'Spotify',
            'itunes'  => 'iTunes',
        );
        foreach($fields as $field => $displayName) {
            if(get_field($field)) {
                $links .= (strlen($links) ? ' | ' : '') . '<a target="_blank" href="' . the_field($field) . '">' . $displayName . '</a>';
            }
        }
        echo $links;
    ?>
    

    The advantage of this code is that it still works with more than 2 fields…