高级自定义字段的三元运算符

我一直在广泛使用”高级自定义字段”创建自己的WP主题, 并且继续像下面这样键入代码:

<?php
if(get_field('featured_courses_heading_text', 'option')) {
    echo get_field('featured_courses_heading_text', 'option');
}
?>

我可以与三元运算符一起使用的此代码的压缩版本吗?我尝试查看文档, 但找不到任何东西


#1


你只需执行此操作即可, 因为没有替代值可输出。

<?php echo get_field('featured_courses_heading_text', 'option'); ?> 

但是, 如果你正在执行if if, 那么你可以执行此操作, 如果它的取值不为空(false), 则将输出该值null, <空字符串>, 0, false

<?php echo get_field('featured_courses_heading_text', 'option') ?: 'nothing here'; ?>

将与

<?php if(get_field('featured_courses_heading_text', 'option')) { echo get_field('featured_courses_heading_text', 'option'); } else { echo 'nothing here'; }  ?>

#2


我不确定为什么需要三元运算符;我认为你的代码很好。

<?php 
    echo get_field('featured_courses_heading_text', 'option') ?: '';
?>

来源:

https://www.srcmini02.com/67489.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_36894.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?