Title: [Plugin: Custom Field Template] insert custom code into a template
Last modified: August 19, 2016

---

# [Plugin: Custom Field Template] insert custom code into a template

 *  [zandercent](https://wordpress.org/support/users/zandercent/)
 * (@zandercent)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/)
 * this doesn’t seem to appear anywhere in the instructions – but I thought people
   might like to know that if you put this in your function.php
 *     ```
       // Get Custom Field Template Values
       function getCustomField($theField) {
       	global $post;
       	$block = get_post_meta($post->ID, $theField);
       	if($block){
       		foreach(($block) as $blocks) {
       			echo $blocks;
       		}
       	}
       }
       ```
   
 * you can use this
 * `<?php getCustomField('customfieldname'); ?>`
 * to insert the output from a custom field into a template…
 * Just thought I’d share.
 * [http://wordpress.org/extend/plugins/custom-field-template/](http://wordpress.org/extend/plugins/custom-field-template/)

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

 *  [Carlo Rizzante](https://wordpress.org/support/users/carlorizzante/)
 * (@carlorizzante)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/#post-1458834)
 * Yeah, it works fine, it seems to me.
 * Thank you very much!
 *  [MartyThornley](https://wordpress.org/support/users/martythornley/)
 * (@martythornley)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/#post-1459080)
 * Thanks for that idea. Posting my little follow up…
 * I wanted to just get a single field and also return the value so i could do what
   I want with it in the template, instead of echoing within the function.
 *     ```
       function getCustomField($theField) {
       	global $post;
       	$field = get_post_meta($post->ID, $theField, true);
       	if($field){
       		return $field;
       	}
       }
   
       function getCustomFields($theField) {
       	global $post;
       	$fields = get_post_meta($post->ID, $theField);
       	if($fields){
       		return $fields;
       	}
       }
       ```
   
 * Then in the template you can
 *     ```
       echo getCustomField($theField)
       ```
   
 * or
 *     ```
       $fields = getCustomField($theField);
   
       foreach ($fields as field {
        // do stuff - ex: echo $field
       }
       ```
   
 * btw – for use with the [MultiEdit](http://wordpress.org/extend/plugins/pagely-multiedit/)
   plugin, the fields take names ‘multiedit_’ followed by whatever name you gave
   it… ‘multiedit_MyFieldname’;
 *  [MartyThornley](https://wordpress.org/support/users/martythornley/)
 * (@martythornley)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/#post-1459081)
 * One other note. When using the file upload for Custom Field Templates, all it
   stores is the ID# of the attached file. You would have to get the value of that
   file (a number) then use the built in wordpress function to get the attached 
   file by its ID.
 * Or you can use this with the above functions…
 *     ```
       function get_custom_file($field) {
        	$ID = getCustomField($field);
       	$file = get_attached_file( $ID );
       	return $file;
       }
       ```
   
 *  [MartyThornley](https://wordpress.org/support/users/martythornley/)
 * (@martythornley)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/#post-1459082)
 * One last update, I swear…
 * So I realized get_file_attachment returns the PATH not the URL. Why? I don’t 
   know. What am I going to do use include($file)??? Makes no sense.
 * What I want is the file’s URL so I can use an image or add a download…
 * Below is everything. I cleaned up the function names too so they are consistent:
 *     ```
       function get_custom_field($theField) {
       	global $post;
       	$field = get_post_meta($post->ID, $theField, true);
       	if($field){
       		return $field;
       	}
       }
   
       function get_custom_fields($theField) {
       	global $post;
       	$fields = get_post_meta($post->ID, $theField);
       	if($fields){
       		return $fields;
       	}
       }
   
       function get_custom_file($field) {
        	$ID = get_custom_field($field);
       	$fullPath = get_attached_file( $ID);
       	$filePath = str_replace (ABSPATH, '', $fullPath );
       	$fileURL = get_bloginfo('siteurl') . '/' . $filePath;
       	return $fileURL;
       }
       ```
   
 *  [fluotonic](https://wordpress.org/support/users/fluotonic/)
 * (@fluotonic)
 * [16 years ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/#post-1459118)
 * Thanks a LOT for all of this ! It will save me so much time !
    I don’t understand
   why it’s not specified on the plugin’s page.
 * Anyway thank’s again !
 *  [Trevor Gehman](https://wordpress.org/support/users/trevorgehman/)
 * (@trevorgehman)
 * [16 years ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/#post-1459120)
 * Yes, thanks for doing this!
 * I added another function based on yours, in case you wanted to return an array
   of files, instead of just a single one:
 *     ```
       // Returns array of URLS of custom field files
       function get_custom_files($field) {
        	$ID = get_custom_fields($field);
       	foreach($ID as $i) {
       		$files[] = get_bloginfo('siteurl') . '/' . str_replace (ABSPATH, '', get_attached_file($i) );
       	}
       	return $files;
       }
       ```
   
 *  [senica@gmail.com](https://wordpress.org/support/users/senicagmailcom/)
 * (@senicagmailcom)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/#post-1459127)
 * Geez! Thank you so much! I spent hours trying to figure out a problem with Page.
   ly MultiEdit. It seems that with 3.0 (for me anyways) $Globals were not being
   registered for the multiedit fields. When I did a var_dump for the multiedit 
   Globals, I got nothing.
 * Anyways, this fixed me right up
 *  [mmtrav](https://wordpress.org/support/users/mikeyleung/)
 * (@mikeyleung)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/#post-1459140)
 * You might also want to mention, for those who love to avoid PHP code, like me,
   that you can use the shortcode [cft format=0] to output the code in the options
   panel. I couldn’t find the spot where it said to use format=0!

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

The topic ‘[Plugin: Custom Field Template] insert custom code into a template’ is
closed to new replies.

 * 8 replies
 * 7 participants
 * Last reply from: [mmtrav](https://wordpress.org/support/users/mikeyleung/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-insert-custom-code-into-a-template/#post-1459140)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
