【发布时间】:2015-10-28 21:18:54
【问题描述】:
所以,我的插件(wordpress)中有以下表单:
if(empty($_POST['post_id'])){
$post = array(
'post_content' => 'Dummy Content',
'post_title' => 'Dummy Title',
'post_status' => 'inherit',
'post_type' => 'post'
);
$post_id = wp_insert_post( $post, true );
}else{
$post_id = $_POST['post_id'];
}
$upload_dir = wp_upload_dir();
$files = $_FILES['files'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$filename = rand() . '' . $files['name'][$key];
$file = array(
//Here goes other irrelevant code
);
$upload_overrides = array( 'test_form' => false );
$image_path[] = array(
'image_path' => $upload_dir['url'] . '/' . $filename,
'meta_key' => 'upload_image',
'meta_value' => $upload_dir['url'] . '/' . $filename,
'post_id' => $post_id
);
add_post_meta($post_id, 'upload_image', $upload_dir['url'] . '/' . $filename );
}
}
echo json_encode($image_path);
exit;
上面的函数将图像添加为带有虚拟内容(例如标题/描述等)的帖子。
现在,如果我想更新内容,可以通过以下方式进行:
global $wpdb;
$tags = $_POST['rh_tags'];
if(!empty($_POST['post_id'])){
$data = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'tags_input' => $tags,
'post_status' => 'publish'
);
$where = array('ID' => $_POST['post_id']);
$update = $wpdb->update($wpdb->prefix . 'posts' , $data, $where, $format = null, $where_format = null );
}else{
$post = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_status' => 'publish',
'tags_input' => $tags,
'post_type' => 'post'
);
wp_insert_post($post);
}
echo 'success';
exit;
这里我添加了'tags_input' => $tags,来添加一个帖子标签。
这是目前的情况:
1.未选择图像: 在输入字段(带有标题、描述和标签的简单输入字段的表单)中,如果我在未选择图像的情况下向标签输入添加内容,则使用标记。
2。已选择图片:如果选择了图片,则使用标签输入,它根本不会创建帖子。
我尝试在第一个函数中添加'tags_input' => $tags,,但这也不起作用。
谁能指出我需要在我的第一个函数中做出哪些改变?
【问题讨论】:
-
我看不到您在第一个 sn-p 中声明
$image_path的位置。 -
我编辑在第一个函数中添加了
$image_path。 -
enable erros =>
ini_set('error_reporting', E_ALL);和ini_set('display_errors', 1);并运行脚本,检查是否有任何错误。