0

I'm trying to make an if-else-statement which works by going if there is a link print it around the name.

I have the below code which is almost there. However, the link is being printed above the text ranther than being an actual link.

<?php if( the_sub_field('corporate_link') ){ ?>

<a href="<?php the_sub_field('corporate_link'); ?>" 
   target="_blank" 
   title="<?php the_field('corporate_name'); ?>"><?php the_field('corporate_name'); ?></a>

<?php } else { ?>

<?php the_sub_field('corporate_name'); ?>

<?php } ?>

Any thoughts on how to make it link instead of printing the link if it`s there?

So what im looking to acheive is if there is a link print this

 <a href="<?php the_sub_field('corporate_link'); ?>">Coprate Name</a>

If there isn't a link it just shows the corporate name.

2
  • I think the_sub_field('corporate_link') is not return any value, and it's always false. But this function print to output buffer. Because, you get it above Commented Dec 4, 2013 at 11:41
  • What do you get when you print the_sub_field('corporate_link') Commented Dec 4, 2013 at 11:44

2 Answers 2

3

use get_sub_field('corporate_link') instead of the_sub_field('corporate_link')

<?php 
   $corporate_link = get_sub_field('corporate_link');
   $corporate_name = get_sub_field('corporate_name');

  if( $corporate_link != '' ){ ?>

   <a href="<?php echo $corporate_link; ?>" 
   target="_blank" 
   title="<?php echo $corporate_name; ?>"><?php echo $corporate_name; ?></a>

 <?php } else { ?>

 <?php echo $corporate_name; ?>

<?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

0

use get_sub_field() and get_field() instead of the_sub_field() and the_field()

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.