• Resolved lemiran

    (@lemiran)


    Hi everyone
    I have a wordpress site under construction and what i wanted to do is quite simple i think… but i’m not very good in php language…

    I already succeed to display my article meta, by adding this code in my content.php:

    <li><span class="element-fiche-technique">Lieu > </span><span class="info-fiche-technique"><?php the_field('lieu'); ?></span></li>

    But i notice that if some article don’t have nothing in their “Lieu” meta, so i have the word “Lieu” anyway…

    I tried to put this php code but it doesn’t seem to be functionnal.

    <?php   
    if(empty(the_field['montant-dh']))  
    {  
        echo "vide" ;  
    }  else { echo "pas vide";} 
    ?>

    Thank you for your precious help!
    Regards
    Miran

Viewing 6 replies - 1 through 6 (of 6 total)
  • Miran,

    I see you are using a function called “the_field”, which comes from Advanced Custom Fields. I’m assuming you are using that plugin (but please confirm).

    If you need to check if the meta value exists, you can use the following code:

    
    <?php
    if ( empty( get_field('montant-dh') ) ) {
    	echo "vide"; //value is empty
    } else {
    	echo "pas vide";
    }
    ?>

    A few notes:

    1. Using “the_field” echo’s the value, which outputs it to the screen. When working with if statements, you want to return the value, which allows you to review it. In your case, you want to return the value to the empty() function to see if it’s available.
    2. In your code, you used square brackets [] instead of parenthesis (). Anytime you are calling a function you should use the format function_name(parameters).
    3. Be sure to use the code above inside of the loop. The function get_field is anticipating the post_id will be provided. If you aren’t running it in the loop, you can use this: get_field('montant-dh', INSERT_POST_ID) (you should place the post id inside the function where I put the placeholder.

    If you have any questions don’t hesitate to ask.
    Tom

    Thread Starter lemiran

    (@lemiran)

    Hi Tom
    Thank you very much for taking time for me ??

    Yes i’m using ACF, which helped me to create some meta.
    Great explanations!! I tried to modify my code and yeah! it works (display “vide” if empty and “pas vide ” if not empty…

    Now i tried a little variation in order to say:
    If my field(‘lieu’) is not empty so display this line.

    <?php if( !empty( get_field(‘lieu’))){
    echo “<span class=”element-fiche-technique”>Montant DH > </span><span class=”info-fiche-technique”><?php the_field(‘lieu’); ?></span>” ;
    }
    ?>

    I stylized it with spans… I don’t know if i’m on the right way ??

    Miran,

    You’re definitely on the right track, but need a few tweaks:

    <?php 
    if ( ! empty( get_field( 'lieu' ) ) ) {
    	echo '<span class="element-fiche-technique">Montant DH</span><span class="info-fiche-technique">'.get_field("lieu").'</span>';
    }
    ?>
    • Be careful when using ‘ and “. You need to make sure they’re standard quotes and not curly quotes.
    • You used echo and then tried to output PHP (<?php the_field() ?>). When you are inside PHP, which is anything between <?php and ?> you should not wrap your PHP code.
    • You are outputting HTML, and you can embed the PHP function inside it by ending the string (I use a single quote and then a dot to concatenate the PHP function get_field).
    • Finally, remember the function the_field automatically echo’s out the value. In your case, you are already using echo, so you want to return the value by using get_field.

    Hope this helps. For further reading, I definitely recommend running through the W3Schools PHP5 Syntax and basics course. It’s quick and will really help in these situations.

    https://www.w3schools.com/php/php_syntax.asp

    Good luck!

    Thread Starter lemiran

    (@lemiran)

    Hi Tom!
    Your help is very precious! I understood many things today… Thank you very much!
    I will take a glance on the link you gave.

    See u and have a nice day
    Miran

    Miran,

    Glad I could help! Be sure to mark the thread as resolved so the administrators know your question was answered.

    Tom

    Thread Starter lemiran

    (@lemiran)

    Yes i’ll mark it now ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hide meta if empty’ is closed to new replies.