简体   繁体   中英

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.

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

使用get_sub_field()get_field()代替the_sub_field()the_field()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM