【问题标题】:wordpress meta-box confusionwordpress 元框混淆
【发布时间】:2014-07-06 00:25:46
【问题描述】:

我的问题是关于save($postID) 函数。这里有一个参数$postID,它在函数中用于保存为id。我看到这个$postID 有一个空值。 $postID 的实际帖子 ID 是如何工作的?

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);
    }

}

【问题讨论】:

  • 你把这个函数放在哪里了。 ?
  • 以上代码放在我的wordpress主题的function.php中。代码工作正常。但是我不明白保存功能的参数是如何工作的?

标签: php mysql wordpress


【解决方案1】:

您使用了错误的 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 以获取详细概述。

您可以查看一些 SO 问题/答案。

保存帖子时

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

一些已经回答的问题

参考

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2017-03-12
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多