si get_post_meta est vide, faites quelque chose
4 réponses
- votes
-
- 2012-06-26
Vouspouvez utiliser lafonctionempty
dans votreif
en tant quetel:<?php if( empty( get_post_meta( $post->ID, 'price_list_category1', true ) ) ) : ?>style="display:none;"<?php endif; ?>
Ce quiprécède renvoie uneerreur,vous devez affecter la valeur de retour à une variable. Voirmamodification ci-dessous.
empty
peutne pas être lameilleure optionen fonction des valeurs que vous stockez dans laméta. Les valeurstelles quefalse
,0
etc ... seront considérées comme vides.Consultez le manuel PHP pour la liste complète des valeurs considéré comme vide.
Vouspouvezessayer d'attribuer laméta à une variable,et de l'utiliser dans l'instruction
if
.<?php $price_list = get_post_meta( $post->ID, 'price_list_category1', true ); ?>
Etpuis ...
if( empty( $price_list) ) : ?>style="display:none"<?php endif; ?>
You could use theempty
function inside yourif
as such :<?php if( empty( get_post_meta( $post->ID, 'price_list_category1', true ) ) ) : ?>style="display:none;"<?php endif; ?>
The above returns an error, you should assign the return value to a variable. See my edit below.
Warning
empty
might not be the best option depending on the values you store in the meta. Values likefalse
,0
etc... will be considered empty.Check the PHP manual for the full list of values that are considered empty.
Edit
You can try assigning the meta to a variable, and using that in the
if
statement.<?php $price_list = get_post_meta( $post->ID, 'price_list_category1', true ); ?>
And then...
if( empty( $price_list) ) : ?>style="display:none"<?php endif; ?>
-
Vouspouvez affecter la valeur de retourméta à une variableet utiliser cette variable dans le `if`.Voirmamodification.You could assign the meta return value to a variable and use that variable inside the `if`. See my edit.
- 0
- 2012-06-26
- Shane
-
Certainesfonctions commeempty ()ne sontpas desfonctions au sensnormal duterme.Ils sontimplémentés sousforme defonctionnalités/mots-clés de langage qui évaluent directement une variable au lieu detransmettre une valeur à unefonction.Some functions like empty() aren't functions in the normal sense. They're implemented as language features/keywords that evaluate a variable directly instead of passing a value to a function.
- 0
- 2015-03-01
- Dave Ross
-
- 2017-06-23
Vouspouvez utiliser
metadata_exists();
(afonctionnépourmoi)pour vérifiertouteméta depublicationet faire ce que vous voulez.// Check and get a post meta if ( metadata_exists( 'post', $post_id, '_meta_key' ) ) { $meta_value = get_post_meta( $post_id, '_meta_key', true ); }
You can use
metadata_exists();
(worked for me)for checking for any post meta and the do whatever you want.// Check and get a post meta if ( metadata_exists( 'post', $post_id, '_meta_key' ) ) { $meta_value = get_post_meta( $post_id, '_meta_key', true ); }
-
- 2014-02-19
J'aitrouvé celaen cherchantmoi-même une solution,maisje me suis rendu compte que la réponse étaittrès simple.Vous devez simplement vérifier si la valeurest vide,siellene fait alors aucun écho - sielle a du contenu,puis affichez le contenu - le code quej'ai utiliséest ci-dessouset peut être adaptéen conséquence àtoutepersonne qui doit l'utiliser.>
<?php $meta = get_post_meta( get_the_ID(), 'page-sub-title', true ); if ($meta == '') { echo ' '; } else { echo '<h2>' . $meta . '</h2>'; } ?>
I found this via searching for a solution myself, but it dawned on me the answer is very simple. You simply need to check if the value is empty, if it is then echo nothing - if it has content, then display the content - the code i used is below and can be tailored accordingly to anyone who needs to use it.
<?php $meta = get_post_meta( get_the_ID(), 'page-sub-title', true ); if ($meta == '') { echo ' '; } else { echo '<h2>' . $meta . '</h2>'; } ?>
-
fonctionnemaispourquoi `get_the_ID ()` au lieu dubon vieux `$post-> ID`works but why `get_the_ID()` instead of good ol `$post->ID`
- 1
- 2014-04-08
- gurung
-
- 2018-08-22
if( ! in_array( 'given_key', get_post_custom_keys($post_id) ) ) {}
Ici,ilest écrit: https://developer.wordpress.org/reference/functions/get_post_meta/# user-contributions-notes
get_post_custom_keys Renvoie untableau contenant les clés detous les champspersonnalisés d'un article ou d'unepageen particulier.Pourmoi,c'est lameilleure solution :)
if( ! in_array( 'given_key', get_post_custom_keys($post_id) ) ) {}
Here it is written: https://developer.wordpress.org/reference/functions/get_post_meta/#user-contributed-notes
get_post_custom_keys Returns an array containing the keys of all custom fields of a particular post or page. For me, this is the best solution :)
J'aibesoin de l'opposé de ceci:
En d'autrestermes,je veux
style="display:none;"
uniquement lorsque lesmétadonnéesn'existentpas.Jepensais que ce serait simple comme
if ( get_post_meta($post->ID, 'price_list_category1', true
mais ce vrai/faux s'avère être une chose complètement différente.desidées?
Merci.