Title: Using Custom Fields In Functions
Last modified: August 19, 2016

---

# Using Custom Fields In Functions

 *  [waterwing](https://wordpress.org/support/users/waterwing/)
 * (@waterwing)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-custom-fields-in-functions/)
 * Hey,
 * This is my first go at posting here but I’m having a problem.
    I have moderate
   PHP knowledge but here is basically what I’m trying to do:
 * Inside my template/theme, let’s say page.php, I am trying to set up a function
   that displays a custom field, but the name of the custom field is the variable.
 * So right now it looks like this at the top of page.php:
 *     ```
       function contentitem($number) {
   
       echo "<h3>"; echo get_post_meta($post->ID, "Name$number", true); echo "</h3>";
   
       }
       ```
   
 * then below, where I want it displayed, I have:
 *     ```
       contentitem("1");
       contentitem("2");
       ```
   
 * So far it isn’t grabbing that variable and using it in the function though. Maybe
   it is just a syntax error but I’m not sure. Thanks.

Viewing 1 replies (of 1 total)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-custom-fields-in-functions/#post-1278526)
 * Try:
 *     ```
       function contentitem($number) {
       	echo "<h3>";
       	echo get_post_meta($post->ID, 'Name'.$number, true);
       	echo "</h3>";
       }
       ```
   
 * If that still doesn’t work, try echoing out $number in the contentitem function
   to check that the variable is being passed correctly.

Viewing 1 replies (of 1 total)

The topic ‘Using Custom Fields In Functions’ is closed to new replies.

## Tags

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

 * 1 reply
 * 2 participants
 * Last reply from: [esmi](https://wordpress.org/support/users/esmi/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/using-custom-fields-in-functions/#post-1278526)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
