0

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.

2
  • 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? Feb 23, 2012 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! Feb 24, 2012 at 1:19

1 Answer 1

2

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

1
  • Yes. This is the solution. Jul 12, 2012 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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