【问题标题】:Wordpress Custom post type + metabox doesn't save dataWordpress 自定义帖子类型 + 元框不保存数据
【发布时间】:2013-04-24 14:40:46
【问题描述】:

当我保存帖子时,metabox 中的数据没有保存在数据库中...错误在哪里?

我的代码在这里:pastebin

谢谢!

【问题讨论】:

    标签: wordpress custom-post-type meta-boxes


    【解决方案1】:

    实际上,您已经在 restaurant_data_form 函数中声明了您的 $rest_custom_meta_fields 数组并尝试在 save_restaurant_custom_meta 函数中使用它,在这种情况下,该数组超出了函数范围,因此 foreach ($rest_custom_meta_fields as $field) 不起作用。

    要克服这个问题,您可以将数组排除在您的 restaurant_data_form 之外,只需在 restaurant_data_form 函数之前声明 array 即可

    $rest_custom_meta_fields = array(  
        array(  
            'label'=> 'Address',  
            'desc'  => 'Plugin use it to get map',  
            'id'    => $prefix.'text_address',  
            'type'  => 'text'  
        ),
        ...
    );
    

    在你的 restaurant_data_form 函数中

    function restaurant_data_form()
    {
        $prefix = 'rest_';
        global $post, $rest_custom_meta_fields;
        // ...
    }
    

    所以它应该看起来像这样(数组在全局范围内)

    $rest_custom_meta_fields = array( 
        array(...),
        ...
    );
    function restaurant_data_form()
    {
        $prefix = 'rest_';
        global $post, $rest_custom_meta_fields;
        // ...
    }
    

    我希望这能解决问题。同样在您使用的代码的末尾

    echo add_action('save_post', 'save_restaurant_custom_meta');
    

    add_action(...) 语句的开头删除echo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-11
      • 2023-01-28
      • 2016-05-23
      • 2017-03-18
      • 2013-06-02
      • 2017-01-03
      • 2012-11-23
      • 1970-01-01
      相关资源
      最近更新 更多