繁体   English   中英

检查三元运算符是否为空

[英]Check if empty in ternary operator

我有一个Turnery运算符,用于检查Wordpress帖子类型是否为链接格式。 如果是,它将输出一个自定义字段,如果不是,则输出固定链接。

我还将如何检查自定义字段是否为空? 因此,如果为空,则输出永久链接,如果不是,则输出自定义字段。

到目前为止,这就是我所拥有的。

<h3><a href="<?php get_post_format() == 'link' ? the_field("external_link") : the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

我正在考虑一些类似的方法,但是似乎没有用。

<h3><a href="<?php get_post_format() == 'link' && the_field("external_link") !="" ? the_field("external_link") : the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

如果值确定,这应该可以工作:

<?php (get_post_format() == 'link' && the_field("external_link")) ? the_field("external_link") : the_permalink(); ?>

这样做的2种方法:

1.在条件周围加上()

<h3><a href="<?php (get_post_format() == 'link' && the_field("external_link") !="") ? the_field("external_link") : the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

2.检查条件并首先将其分配给变量,然后再使用

<?php $link = (get_post_format() == 'link' && the_field("external_link") !="") ? the_field("external_link") : the_permalink();
<h3><a href="<?php echo $link; ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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