简体   繁体   中英

How to write if statement for get_post_meta in Wordpress

I'm trying to right an if statement for get_post_meta. I want to display the info if something is there, if there is nothing, don't display anything. Here is an example I am working with, with no luck.

if(get_post_meta()); echo '<a href="'.get_post_meta($post->ID, 'text', true).'">'; endif;

Any help is appreciated. Thanks.

You've made an error in the syntax. Use

if($condition): statements; endif;
              ^

instead of

if($condition); statements; endif;
              ^

So your code would be

if(get_post_meta()):
    echo '<a href="'.get_post_meta($post->ID, 'text', true).'">';
endif;

That's the alternative syntax for control structures .
You can always use the standard one

if($conition) {
    // statements
    // .....
}
else { 
    // otherweise
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM