Created
March 22, 2012 07:55
-
-
Save byronrode/2156928 to your computer and use it in GitHub Desktop.
Adding this to your functions.php will allow you to use custom field values in your post via short code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
@description: Adding this to your functions.php will allow you to use custom field values in | |
your post via shortcode. Useful for when you need to add javascript to a post. | |
@usage: [custom_field name="name_of_custom_field"] | |
@returns: Custom Field Value | |
@author: Byron Rode | |
@license: Free to use, modify, whatever. | |
*/ | |
if(!is_admin()) add_action('init', 'tgr_add_shortcodes'); | |
if(!function_exists('tgr_add_shortcodes')){ | |
function tgr_add_shortcodes(){ | |
add_shortcode('custom_field', 'tgr_register_shortcode'); | |
} | |
} | |
if(!function_exists('tgr_register_shortcode')){ | |
function tgr_register_shortcode($atts){ | |
global $post; | |
// defaults | |
extract( shortcode_atts( array('name' => ''), $atts ) ); | |
if($name == '') return; | |
$post_id = $post->ID; | |
$custom_fields = get_post_custom($post_id); | |
if(!empty($custom_fields[$name][0])){ | |
return $custom_fields[$name][0]; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment