解析错误:语法错误,意外T_OBJECT_OPERATOR,期待')'

时间:2013-07-28 16:24:08

标签: php wordpress syntax error-handling syntax-error

我不知道,这段代码出了什么问题:

function my_wpcf7_save($cfdata) {

$formtitle = $cfdata->title;
$formdata = $cfdata->posted_data;   

if ( $formtitle == 'contactform1') {

    // access data from the submitted form
    $formfield = $formdata['name'];

    // create a new post
    $newpost = array( 
                  'post_title' -> $formdata['name']);
                  'post_content' -> $formdata['message']);
                  'post_status' -> 'publish');

    $newpostid = wp_insert_post($newpost);

    // add meta data for the new post
    add_post_meta($newpostid, 'email', $formdata['email']);
    add_post_meta($newpostid, 'subject', $formdata['subject']);
}

}
add_action('wpcf7_before_send_mail', 'my_wpcf7_save',1);

我收到了错误: 解析错误:语法错误,意外T_OBJECT_OPERATOR,期待')' ...对于这一行:'post_title' - > $ FORMDATA [ '名称']);

据我所知,语法是对的,不是吗?

2 个答案:

答案 0 :(得分:3)

请在您的问题中阅读马里奥的评论:

// create a new post
$newpost = array( 
              'post_title' => $formdata['name'],  <------------------ Here
              'post_content' => $formdata['message'], <-------------- Here
              'post_status' => 'publish');

更新:同时将->替换为=>,如上所示。

答案 1 :(得分:2)

我很确定这个:

$newpost = array( 
  'post_title' -> $formdata['name']);
  'post_content' -> $formdata['message']);
  'post_status' -> 'publish');

应该是:

$newpost = array( 
  'post_title' => $formdata['name'],
  'post_content' => $formdata['message'],
  'post_status' => 'publish');

在实际完成声明数组之前,你正在关闭数组和语句。这就是我认为你至少要做的事情。