Php 检查三值运算符中是否为空
我有一个turnery操作符,检查Wordpress帖子类型是否为链接格式。如果是,则输出自定义字段,如果不是,则输出永久链接 我如何检查自定义字段是否为空?因此,如果为空,则输出永久链接,如果不是,则输出自定义字段 这就是我目前所拥有的Php 检查三值运算符中是否为空,php,wordpress,Php,Wordpress,我有一个turnery操作符,检查Wordpress帖子类型是否为链接格式。如果是,则输出自定义字段,如果不是,则输出永久链接 我如何检查自定义字段是否为空?因此,如果为空,则输出永久链接,如果不是,则输出自定义字段 这就是我目前所拥有的 <h3><a href="<?php get_post_format() == 'link' ? the_field("external_link") : the_permalink(); ?>" rel="bookmark" t
<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>
在条件周围添加()
并检查:-(获取帖子格式()='link'&字段(“外部链接”)!=“”