probablybest
probablybest

Reputation: 1445

Wordpress php else if statement with advanced custom fields

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.

Upvotes: 0

Views: 3020

Answers (2)

Valery  Statichny
Valery Statichny

Reputation: 593

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

Upvotes: 0

Pranita
Pranita

Reputation: 1325

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

Upvotes: 3

Related Questions