• 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.

    https://www.ads-software.com/extend/plugins/custom-field-template/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Yeah, it works fine, it seems to me.

    Thank you very much!

    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 plugin, the fields take names ‘multiedit_’ followed by whatever name you gave it… ‘multiedit_MyFieldname’;

    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;
    }

    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;
    }

    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 !

    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;
    }

    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

    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.