我想为我的网站建立一个基本的“评级”系统-我不想使用插件-所以我想在每个新的自定义帖子中添加自定义字段“评级”,并用数字1填充此字段。
这可能吗?还是我走错了方向?做了很多搜索发现的不多,add_post_meta()
? 我不知道那会去哪里。
我想为我的网站建立一个基本的“评级”系统-我不想使用插件-所以我想在每个新的自定义帖子中添加自定义字段“评级”,并用数字1填充此字段。
这可能吗?还是我走错了方向?做了很多搜索发现的不多,add_post_meta()
? 我不知道那会去哪里。
您可以通过将一个简单的函数挂接到save_post
钩
add_action(\'save_post\',\'my_rating_field\');
function my_rating_field($post_id){
global $post;
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) )
return;
// Check post type
if ($post->post_type != "YOUR_POST_TYPE_NAME")
return;
// OK, we\'re authenticated: we need to find and save the data
$rating = get_post_meta($post_id,\'rating\',true);
//if field not exists create it and give it the value of one
if (empty($rating) || !isset($rating)){
update_post_meta($post_id,\'rating\',1);
}
}
以下是场景:在我的插件中,我想在帖子/页面编辑器上显示一个元框。我希望metabox完全像WordPress的“自定义字段”metabox(添加另一个、删除、更新、自动填充下拉列表、输入新链接、AJAX magic等);有几个细微的区别:我要的不是“Name”和“Value”,而是“Target”、“Name”和“Value”与其将它们全部保存为自动显示在WP自定义字段元框中的“公共”自定义字段,不如将它们全部存储为“专用”命名空间字段中的单个多维数组。。。即。,_myplugin_custom_fiel