wordpress meta-box混淆 [英] wordpress meta-box confusion

查看:132
本文介绍了wordpress meta-box混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是在save($postID)函数上.这里有一个参数$postID,该参数在该函数中用于保存为id.我看到此$postID具有空值.实际的帖子ID如何用于$postID?

My question is on save($postID) function. Here there is a parameter $postID which is using in the function for saving as id. I see that this $postID has a null value. How does actual post id work for $postID?

This is the simple meta-box code

/* simple meta box */

/* creating field */

add_action('admin_menu', 'my_post_options_box');

function my_post_options_box() {
    if ( function_exists('add_meta_box') ) { 

        add_meta_box('post_header', 'Asif, save me!', 'testfield', 'post', 'normal', 'low');
    }
}


function testfield(){
global $post;
?>
<input type="text" name="Asif" id="Asif" value="<?php echo get_post_meta($post->ID, 'Sumon', true); ?>">
<?php
}

/* end of creating field */

/* storing  field after pressing save button */
add_action('save_post', 'save');
function save($postID){


    if (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE) {
        return $postID;
    }
    else
    {       
        if($parent_id = wp_is_post_revision($postID))
        {
        $postID = $parent_id;
        }

        if ($_POST['Asif']) 
        {
            update_custom_meta($postID, $_POST['Asif'], 'Sumon');
        }

    }

  }



 // saving in postmeta table
  function update_custom_meta($postID, $newvalue, $field_name){
    if(!get_post_meta($postID, $field_name)){
 // create field
    add_post_meta($postID, $field_name, $newvalue);
     }
   else{
//update field
     update_post_meta($postID, $field_name, $newvalue);
    }

}

推荐答案

您使用了错误的操作挂钩.

You've used the wrong Action Hook.

代替使用add_action('admin_menu', 'my_post_options_box');

使用add_action('add_meta_boxes', 'my_post_options_box');

add_action('add_meta_boxes', 'my_post_options_box');

function my_post_options_box() {
    if ( function_exists('add_meta_box') ) { 

        add_meta_box('post_header', 'Asif, save me!', 'testfield', 'post', 'normal', 'low');
    }
}

您可以访问 http://codex.wordpress.org/Function_Reference/add_meta_box以获得详细概述.

You can look in to http://codex.wordpress.org/Function_Reference/add_meta_box for detailed overview.

您可以研究一些SO问题/答案.

Some SO Question/Answers you can look into.

  • Add multiple dates to custom post type in Wordpress
  • how to add a meta box to wordpress pages

操作save_post自动将Post ID传递给回调函数.您可以在回调函数中使用它.

Action save_post automatically passes Post ID to callback function. Which you can use inside your callback function.

一些已经回答了问题

参考

这篇关于wordpress meta-box混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆