1

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>
1
  • Add () around condition and check:- (get_post_format() == 'link' && the_field("external_link") !="") Commented Apr 25, 2019 at 11:39

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

This should work if values are ok:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.