Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I’m designing a site for a client and I’m using custom fields for buy links to her books. I only want buy buttons to appear for fields that contain a URL, so I’ve hooked this just below the_content:

<?php if( get_field('kindle') ): ?>
           <a href="<?php the_field('kindle'); ?>"><img src="url.com"></a><?php endif; ?>

<?php if( get_field('nook') ): ?>
           <a href="<?php the_field('nook'); ?>"><img src="url.com"></a><?php endif; ?>

This code works perfectly but I would like to be able to customize the position of the buy links a little more. I thought of creating a shortcode to insert into the content of the post, but while I can create one that includes just text, when I put the above code into the shortcode.php file, it breaks.

This is the code I'm using for shortcode.php:

    <?php
function buy_links(){


<?php if( get_field('kindle') ): ?>
               <a href="<?php the_field('kindle'); ?>"><img src="url.com></a><?php endif; ?>

<?php if( get_field('nook') ): ?>
               <a href="<?php the_field('nook'); ?>"><img src="url.com></a><?php endif; ?>

<?php if( get_field('kobo') ): ?>
               <a href="<?php the_field('kobo'); ?>"><img src="url.com></a>
<?php endif; ?>

<?php if( get_field('itunes') ): ?>
               <a href="<?php the_field('itunes'); ?>"><img src="url.com></a>
<?php endif; ?>

}// End buy_links()

?>
share|improve this question
1  
possible duplicate of short code output too early –  Milo Apr 30 at 16:13
    
Why hardcode URL's into your function? –  Brad Dalton Apr 30 at 17:07
    
Only because I'm testing right now. It will all be relative links once I know it's working –  CissyH Apr 30 at 17:14
    
@CissyH In which shortcode.php file did you place this code? It is enough if you place it in functions.php and call the function with add_shortcode( 'buylinks', 'buy_links' );. Then you can use the shortcode [buylinks] anywhere in the post. Also "assign" the HTML code to a variable and return it at the end of the function. –  A.Jesin Apr 30 at 18:00
    
I created a buylinks_shortcode.php file and referenced it in functions.php I thought it might be cleaner in the long run, but either would work just as well I think. Thanks! –  CissyH Apr 30 at 19:16

1 Answer 1

I thought of creating a shortcode to insert into the content of the post, but while I can create one that includes just text, when I put the above code into the shortcode.php file, it breaks.

Your code outputs data to the screen. You can't do that with filters like the_content. You need to concatenate a string and return it. Something like:

function the_content_cb($content) {
  $str = '';
  if( get_field('kindle') ) {
    $str .= '<a href="'.get_field('kindle').'"><img src="url.com"></a>';
  }

  if( get_field('nook') ) {
    $str .= '<a href="'.get_field('nook').'"><img src="url.com"></a>';
  }
  return $content.$str;
}

Note: the_field() is a plugin function-- Advanced Custom Fields--, as is get_field().

share|improve this answer
    
Thanks for responding! That is getting me much closer. The code isn't breaking the site, but it's not displaying anything either. I tried looking up get_the_field and I'm not finding anything outside ACF (which I am using). I did find this on a blog discussing how to use custom fields conditionally. Does it tell you anything? // display an image based on custom-field value, if it exists <?php $image = get_post_meta($post->ID, 'url', true); if($image) : ?> <img src="<?php echo $image; ?>" alt="" /> <?php endif; ?> Thanks again for your help. –  CissyH Apr 30 at 17:14
1  
The correct ACF function is get_field. –  Milo Apr 30 at 17:20
    
Okay!! The code works with a href="'.get_field Thank you both so much. You saved my head from exploding right off my shoulders. Way too many hours working on this :) –  CissyH Apr 30 at 18:58
    
Will edit that. Thanks, @Milo. –  s_ha_dum Apr 30 at 19:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.