Wordpress meta boxes - WP tuts tutorial

扶醉桌前 提交于 2019-12-12 03:10:09

问题


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.



来源:https://stackoverflow.com/questions/9530656/wordpress-meta-boxes-wp-tuts-tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!