caffeinehigh
caffeinehigh

Reputation: 309

Check if empty in ternary operator

I have a turnery operator that checks if a Wordpress post type is of link format. If it is it outputs a custom field and if it isn't it outputs the permalink.

How would I go about also checking if the custom field is empty? So that if it is empty the permalink is output and if it isn't the custom field is output.

This is what I have so far.

<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>

I am thinking something along the lines of this but it doesn't seem to work.

<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>

Upvotes: 1

Views: 1185

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

2 ways to do so:-

1.Add () around condition

<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.Check condition and assign it to variable first, then use it next

<?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>

Upvotes: 1

Vlad K
Vlad K

Reputation: 143

This should work if values are ok:

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

Upvotes: 1

Related Questions