1

I'm displaying values from a custom taxonomy on single pages of my custom post type.

I have a custom post type "artwork" with a taxonomy "artists". How I display the artist name on the single post page (or multiple artists names if multiple artists are assigned to the post). If no artist is assigned to the post, no artist is shown on the page. That works fine.

<?PHP //  ARTIST(s) attached to Artwork

    $artists = get_the_terms( get_the_ID(), 'artists' );
    $artistsArray = array();

    if( ! empty( $artists ) ) : 
        echo "<div>Artist: <strong>";
        foreach ( $artists as $artist ){ 
            $artistsArray[] = '<span><a href="/artists/'.ucfirst($artist->slug).'">'.ucfirst($artist->name).'</a></span>';
        }
        echo implode( ', ', $artistsArray );
        echo "</strong><div>";  
    endif; ?>

In my artists taxonomy, I have custom field "artist instagram". Further down on the single blog post, I have a link that leads to the instagram account of the artist.

<?php
    $artistigs = get_the_terms( get_the_ID(), 'artists' );
    if( ! empty( $artistigs ) ) : ?>    
    <?php foreach( $artistigs as $artistig ) : ?>                   
        <div><i class="fab fa-instagram"></i> <strong><a href="https://instagram.com/<?php the_field('artist-instagram', $artistig); ?>" target="_blank">@<?php the_field('artist-instagram', $artistig); ?></a></strong></div>
    <?php endforeach; 
    endif;
?>

The link is displayed correctly. But I want to hide it when an artist has no instagram. I'm using if( ! empty() ), but it's still showing the link if an artist's instagram field is empty. Why?

Thank you for your help!


EDIT UPDATE:

With this code, the Instagram link only shows up on posts assigned to artists with an IG link (as desired). The problem: It only outputs $artistig and all other text/HTML within foreachis ignored (e.g. the TEST text is not put out).

<?php 
    $artists = get_the_terms( get_the_ID(), 'artists' );
    if( !empty( $artists ) ) :  
        foreach( $artists as $artist ) :        
            $artistig = the_field( 'artist-instagram', $artist );
            if( !empty( $artistig ) ) : 
                echo "IG User TEST: ". var_dump( $artistig ) ." END TEST";
            endif;      
        endforeach;
    endif; 
?>

1 Answer 1

2

You're not running check on whether the artist's instagram field is empty, you're running a check on whether the 'artists' term is empty, which it isn't.

<?php
    $artistigs = get_the_terms( get_the_ID(), 'artists' );
    if( ! empty( $artistigs ) ) : ?>
         //This will execute if the artist term is not empty
    endif;
?>

I don't use ACF myself so not 100% sure how stuff like the_field() works but try this:

<?php
    $artists = get_the_terms( get_the_ID(), 'artists' );
    if( !empty( $artists ) ) : ?>    
    <?php foreach( $artists as $artist ) :
        $artistig = get_field( 'artist-instagram', $artist );
        if( !empty( $artistig ) ) :
            echo '<div>';
                echo '<i class="fab fa-instagram"></i> <strong><a href="https://instagram.com/' . $artistig . '" target="_blank">@' . $artistig . '</a></strong>';
            echo '</div>';
        endif;
   endforeach; 
  endif;
?>

================

UPDATE

To address your update, the var_dump() is really going to mess with formatting, so lets just remove it.

<?php 
    $artists = get_the_terms( get_the_ID(), 'artists' );
    if( !empty( $artists ) ) :  
        foreach( $artists as $artist ) :        
            $artistig = get_field( 'artist-instagram', $artist );
            if( !empty( $artistig ) ) : 
                echo 'IG User TEST: '. $artistig .' END TEST';
            endif;      
        endforeach;
    endif; 
?>
8
  • Thank you! It works in general, as the value is only displayed on posts from artists with an Instagram account. But the output is only the value of the custom field – all text and HTML within foreach is ignored.
    – HAL2020
    Apr 10, 2020 at 23:07
  • That may be something about ACF... ...maybe we need to try just echoing everything? What is the output if you var_dump( $artistig );? Apr 10, 2020 at 23:15
  • I wasn't able to manage it yet. I posted an update with the new code in the original post. This only puts out the `$artistig' value but ignores the test text.
    – HAL2020
    Apr 11, 2020 at 1:58
  • Remove the var_dump() and just leave the . $artistig . Apr 11, 2020 at 2:00
  • Same problem. This only puts out the $artistig value, but not the text/HTML. :(
    – HAL2020
    Apr 11, 2020 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.