最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Hide custom field if empty

programmeradmin4浏览0评论

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="/<?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; 
?>

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/<?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; 
?>
Share Improve this question edited Apr 11, 2020 at 1:54 HAL2020 asked Apr 10, 2020 at 16:00 HAL2020HAL2020 398 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/' . $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; 
?>
发布评论

评论列表(0)

  1. 暂无评论