Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is this possible? If so, can someone point me towards a clear tutorial? I have a column of content I want to float right of the_content, so I'd like to be able to use a shortcode to put that content in the post for a page.

I've found the following (but want to modify it for get_post_meta):

function get_custom_field_fn() {    
 global $post; 
 $custom_field_value = get_post_meta($post->ID, 'cf_name', true); 
 return $custom_field_value; 
} 

add_shortcode('cf_name', 'get_custom_field_fn');

I'm guessing that I need to change a few things but not sure which things to change. My custom meta boxes will have 8 fields. I'm also guessing that my shortcode will need to allow for the post/page ID and I also need to interject some HTML in the function.

share|improve this question
    
Can you clarify what problems you're having? Your example code looks correct... are you just having a hard time customizing it with the custom fields you want to use? Or are you trying to remove the whole function from the content so it can be displayed in a sidebar? –  goldenapples Feb 23 '12 at 21:14
    
My main issue is how to change the code so I can use it with get_post_meta and how to include HTML in the function itself. Make more sense? Thanks! –  Justice Is Cheap Feb 24 '12 at 1:19
add comment

1 Answer 1

up vote 2 down vote accepted

yeah, the question is a bit unclear. but i think you need shortcode attributes

function get_custom_field_fn( $atts) {    
global $post; 

 extract( shortcode_atts( array(
        'meta' => ''
    ), $atts ) );


 $custom_field_value = get_post_meta($post->ID, $meta, true); 
 return $custom_field_value; 
} 

add_shortcode('cf_name', 'get_custom_field_fn');

see the codex: http://codex.wordpress.org/Shortcode_API#Attributes

share|improve this answer
    
Yes. This is the solution. –  Monirul Islam Jul 12 '12 at 5:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.