0

I have a simple question. I have this code and its correct,

<?php global $wp_query;
$postid = $wp_query->post->ID;
$meta = get_post_meta($postid, 'codigopostal', true);
if($meta != $empty) { echo "(".$meta.")"; } ?>

but when the user don't put nothing, value is 0. I want delete that value on frontpage.

<?php global $wp_query;
$postid = $wp_query->post->ID;
$meta = get_post_meta($postid, 'codigopostal', true);
if($meta != $empty) {
    echo "(".$meta.")";
} 
else if($meta != 0) {
    echo "";
}
?>

I added else if... but is wrong.

closed as off-topic by gmazzap, Wyck, s_ha_dum, tfrommen, fuxia Dec 29 '13 at 23:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – gmazzap, s_ha_dum, tfrommen, fuxia
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I edited my question @G.M. Sorry for my english. If i put your code, give me errors in code. – Vektor Unbreakable Dec 29 '13 at 18:48
  • i changed code of else but still not working. else if($meta != 0) { echo ""; } @G.M. – Vektor Unbreakable Dec 29 '13 at 19:02
0

This question is off-topic because your is a simple PHP error, not related to WordPress.

You are trying to check if a variable is empty using:

if ( $meta != $empty ) { 

but this is not the right way, in fact this code compare the target variable $meta with another variable $empty that is not defined.

If you want to check if a variable is empty in PHP you have to use the empty function so your code should be:

<?php
$postid = get_queried_object_id();
$meta = get_post_meta($postid, 'codigopostal', true);
if ( ! empty($meta) ) echo "(" . $meta . ")";
?>

Note I've used get_queried_object_id that return the id of the current queried post id without using global variables.

  • but with this code, in other post don't show nothing, including post with value. "(" . $meta . ")" don't show never. – Vektor Unbreakable Dec 29 '13 at 19:09
  • Where you are using the code? in which template file? @VektorUnbreakable – gmazzap Dec 29 '13 at 19:10
  • index.php , single-pro.php , archive.php – Vektor Unbreakable Dec 29 '13 at 19:11
  • the problem is, when the user don't put value in that field, the value is 0 and i want when value is 0, don't show nothing, but if value is other, show "(" . $meta . ")" – Vektor Unbreakable Dec 29 '13 at 19:12
  • don't worry, the problem is because custom fields plugin created 0 value in number field. I puted that field like text and problem solved, not the best fixed but... really thanks – Vektor Unbreakable Dec 29 '13 at 19:23

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