Wordpress meta boxes - WP tuts tutorial

2019-08-26 10:36发布

问题:

I did these tree tutorials to create custom metaboxes.

http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/ http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-2-advanced-fields/ http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-3-extra-fields/

but i don't know how to call values from individual fields. I used this php script $meta = get_post_meta($post->ID, $field['custom_text'], true); echo $meta;

but id doesn't work. Does someone know what I do wrong.

回答1:

Well, it's difficult to say without seeing how you have implemented your custom metaboxes - there may well be an issue there - but in the mean time, check the WordPress codex to ensure you are using the get_post_meta() function correctly. The second argument should be a string which represents the key (name) of the meta field you are retrieving.

From the Codex:

$meta_values = get_post_meta($post_id, $key, $single);

// where $key = A string containing the name of the meta value you want.

So double check that the value you're passing in ($field['custom_text']) does actually contain a string representing the name of the meta field you're trying to retrieve.



回答2:

As previous poster stated, you're using get_post_meta wrong. Say you created a custom field inside your custom meta box named "custom_field", you would get the value of said field with this code:

$field_value = get_post_meta($post_id, 'custom_field', true);
echo $field_value; // outputs the field value.

If this doesn't work you're either got the name of the field wrong, or you did something wrong when adding your metabox, if this is the case check your php error log for errors.