Title: Trying to include a custom field code into a function
Last modified: August 20, 2016

---

# Trying to include a custom field code into a function

 *  [dangle2k](https://wordpress.org/support/users/dangle2k/)
 * (@dangle2k)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/trying-to-include-a-custom-field-code-into-a-function/)
 * Caveat: I’m not a programmer! : )
 * Ok, so, through much searching I’ve put this function below together to create
   a shortcode so that my client can specify which posts she wants on the homepage.
   So all she has to do when she wants a particular number of posts on the homepage
   is put this [featured post=322]. Here is the code I used:
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 * Now sometimes there is a credit for a photographer that will go under the author
   byline. So I found this bit of code below for a custom field that will only show
   if this custom field is used. If I don’t use this custom field nothing appears
   in the post or archive pages, which is good. Here is this code:
 *     ```
       <?php if( get_post_meta($post->ID, "photographer", true) ): ?>
       	Photos by <a href="<?php echo get_post_meta($post->ID, "photographer_link", true); ?>"><?php echo get_post_meta($post->ID, "photographer", true); ?></a>
       <?php endif; ?>
       ```
   
 * What I need help with is how do I get that photographer custom field code into
   the featured post function underneath where the author byline is? I can add this
   in
 * `$return .= '<div id="featured-wide-author">Photos by <a href="'.get_post_meta(
   $post->ID, "photographer_link", true).'">' .get_post_meta($post->ID, "photographer",
   true). '</a></div><br />';`
 * which works but of course the “Photos by” will always be showing whether or not
   I use the custom field. Basically I need the if/endif part to be in there.
 * I hope this makes sense and I hope someone can help me with this. Is this even
   possible?
 * Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Jackson Whelan](https://wordpress.org/support/users/madjax/)
 * (@madjax)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/trying-to-include-a-custom-field-code-into-a-function/#post-2281592)
 * Your code was moderated, use wordpress.pastebin.com
 *  Thread Starter [dangle2k](https://wordpress.org/support/users/dangle2k/)
 * (@dangle2k)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/trying-to-include-a-custom-field-code-into-a-function/#post-2281673)
 * Ok, here is the link to the code in pastebin.com
 * [http://pastebin.com/ckf3gW7j](http://pastebin.com/ckf3gW7j)
 * Still looking for help on this!
 * thanks again.
 *  [Jackson Whelan](https://wordpress.org/support/users/madjax/)
 * (@madjax)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/trying-to-include-a-custom-field-code-into-a-function/#post-2281674)
 * You can still wrap it in the same conditional statement, something like:
 *     ```
       if( get_post_meta( $post->ID, 'photographer', true ) ) {
           $return .= '<div id="featured-wide-author">Photos by <a href="'.get_post_meta($post->ID, "photographer_link", true).'">' .get_post_meta($post->ID, "photographer", true). '</a></div><br />';
       }
       ```
   
 * HTH

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Trying to include a custom field code into a function’ is closed to new
replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [Jackson Whelan](https://wordpress.org/support/users/madjax/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/trying-to-include-a-custom-field-code-into-a-function/#post-2281674)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
