WordPress.org

Forums

[Plugin: Custom Field Template] insert custom code into a template (9 posts)

  1. zandercent
    Member
    Posted 5 years ago #

    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/

  2. Carlo Rizzante
    Member
    Posted 5 years ago #

    Yeah, it works fine, it seems to me.

    Thank you very much!

  3. MartyThornley
    Member
    Posted 5 years ago #

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

  4. MartyThornley
    Member
    Posted 5 years ago #

    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;
    }
  5. MartyThornley
    Member
    Posted 5 years ago #

    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;
    }
  6. Fluotonic
    Member
    Posted 4 years ago #

    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 !

  7. Trevor Gehman
    Member
    Posted 4 years ago #

    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;
    }
  8. senica@gmail.com
    Member
    Posted 4 years ago #

    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

  9. mmtrav
    Member
    Posted 4 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic