Title: Echo Custom Field Key Inside PHP Echo Statement
Last modified: August 21, 2016

---

# Echo Custom Field Key Inside PHP Echo Statement

 *  Resolved [Brian](https://wordpress.org/support/users/bkurious/)
 * (@bkurious)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/echo-custom-field-key-inside-php-echo-statement/)
 * Hopefully someone far more experienced working with theme files can help me.
 * I need to echo a custom field value inside of an already existing echo statement
   in my theme files. It seems straight-forward enough, but no amount of Googling
   has helped.
 * Here’s the code I’m working with:
 *     ```
       <?php $product_id = get_post_meta($post->ID, 'product_id', true);if ($product_id) { ?>
       <?php echo get_button_code_for_product(<?php echo $product_id; ?>); ?>
       <?php} ?>
       ```
   
 * The original line I started with was
 *     ```
       <?php echo get_button_code_for_product(1); ?>
       ```
   
 * I need to be able to echo each post’s product_id into the parentheses of the 
   above code.
 * Any help? Thanks in advance!

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

 *  [ronangelo](https://wordpress.org/support/users/ronangelo/)
 * (@ronangelo)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/echo-custom-field-key-inside-php-echo-statement/#post-5115181)
 * You can’t echo inside an echo and guessing on what you’re trying to achieve above,
   you don’t have to. Also, you have php tags inside existing php tags.
 * This is how I would write it:
 *     ```
       <?php
          $product_id = get_post_meta($post->ID, 'product_id', true);
   
          if ( $product_id ) {
             echo get_button_code_for_product( $product_id );
          }
       ?>
       ```
   
 *  Thread Starter [Brian](https://wordpress.org/support/users/bkurious/)
 * (@bkurious)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/echo-custom-field-key-inside-php-echo-statement/#post-5115188)
 * Works like a charm. Thank you!

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

The topic ‘Echo Custom Field Key Inside PHP Echo Statement’ is closed to new replies.

## Tags

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

 * 2 replies
 * 2 participants
 * Last reply from: [Brian](https://wordpress.org/support/users/bkurious/)
 * Last activity: [11 years, 10 months ago](https://wordpress.org/support/topic/echo-custom-field-key-inside-php-echo-statement/#post-5115188)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
