• Resolved nessler

    (@nessler)


    I am trying to write a code that makes a span class disappear if the custom field is empty. Via css I’m adding a comma, since the field concertlocation comes behind earlier metadata. (so; “august 20 2018, Pukkelpop” should be an example of end result)

    However if I program it like the code below, the span class simply doesn’t show up at all. the this->post_ID is necessary since the data is to be shown on an overview page, where some articles don’t have that field “concertlocatie”. What am I missing here, anyone ?

                        <span class="td-author-date">
                            <?php if( $author_photo != '' ) { echo $this->get_author_photo(); } ?>
                            <?php echo $this->get_author();?>
                                                        <?php echo $this->get_date();?>
                            <?php $concertlocatie = the_field( "concertlocatie" , $this->post->ID ); //etc...?>
                            <?php if ( $concertlocatie ) : ?><span class="metacelocation"><?php echo $concertlocatie; ?><?php endif; ?></span>
                            <?php echo $this->get_comments();?>
                        </span>
                    </div>

    the css

    .home .metacelocation:before, 
    .category-306 .metacelocation:before {
    content: ', ';
    text-transform: none;
    font-weight: normal;
    color: #aaa;
    display: inline-block;
    position: relative;
    top: 2px;
    }

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    Try this code :

    <span class="td-author-date">
          <?php if( $author_photo != '' ) { echo $this->get_author_photo(); } ?>
           <?php echo $this->get_author();?>
         <?php echo $this->get_date();?>
          <?php $concertlocatie = the_field( "concertlocatie" , $this->post->ID ); //etc...?>
          <?php if ( $concertlocatie ) : ?><span class="metacelocation"><?php echo $concertlocatie; ?></span><?php endif; ?>
            <?php echo $this->get_comments();?>
    </span>

    You were placing <?php endif; ?> before </span>

    Please correct if i am wrong ??

    Thread Starter nessler

    (@nessler)

    I adjusted it like that, but sadly to no avail, the span class metacelocation is still not showing. Very odd

    Hello,

    Can you please try get_field instead of the_field ?

    Ref : https://www.advancedcustomfields.com/resources/get_field/

    Thread Starter nessler

    (@nessler)

    If I use get field, the comma appears where it should including the span class, but sadly the value of the field “concertlocatie” changes into “ARRAY”, which was the main reason I used the_field.

    This can be seen under the foto of Grate Van Fleet here: https://v2.enola.be/

    Moderator bcworkz

    (@bcworkz)

    get_field() really is the proper function if you want the value returned and not immediately echoed. If you are getting ARRAY on output, temporarily insert
    <?php print_r( $concertlocatie ); ?>, then review the resulting output. Determine the key associated with the value you want to output. Use the key to target what part of the array to output. For example, if we find the key is “term_name”, do this in your code:
    <?php echo $concertlocatie['term_name']; ?>

    Thread Starter nessler

    (@nessler)

    YOU’RE A LIFESAVER !!!!

    the print r function revealed this
    ” [0] => Ancienne Belgique, Brussel ”

    I found it odd to be just simply [0], but sure enough after inserting this into the code the span class AND additional comma appeared like magic.

    After literally spending 2 days cracking this nut I can’t thank you enough.

    If anybody else ever comes across this problem, this was my final solution

    <span class="td-author-date">
     <?php if( $author_photo != '' ) { echo $this->get_author_photo(); } ?>
     <?php echo $this->get_author();?>
     <?php echo $this->get_date();?>
     <?php $concertlocatie = get_field( "concertlocatie" , $this->post->ID ); //etc...?>
     <?php if ( $concertlocatie ) : ?><span class="metacelocation"><?php echo $concertlocatie['0']; ?></span><?php endif; ?>
     <?php echo $this->get_comments();?>
    </span>
    Moderator bcworkz

    (@bcworkz)

    I’m glad you found a solution!

    FWIW, to be 100% correct, don’t use quotes for array index numbers, only associative array keys. $concertlocatie[0]
    It works with or without because PHP is a loosely typed language. The difference between strings and integers and other data types is not strictly enforced. But since the array was indexed, the 0 is an integer and no quotes is the proper expression.

    I was expecting an associative array keyed by strings:
    ” [‘term_name’] => Ancienne Belgique, Brussel ”
    hence the quotes. I expected wrong ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Make span class disappear when custom field is empty in wordpress’ is closed to new replies.