Php 从前端将多个图像上载到帖子
我已经参考了这个链接来为一篇文章添加多个图像。 代码正在运行,即图像正在上载到媒体库,但未显示在帖子中。请告诉我是否必须在帖子中创建一个字段来存储它们。如果是,怎么做 提交帖子的代码:Php 从前端将多个图像上载到帖子,php,wordpress,Php,Wordpress,我已经参考了这个链接来为一篇文章添加多个图像。 代码正在运行,即图像正在上载到媒体库,但未显示在帖子中。请告诉我是否必须在帖子中创建一个字段来存储它们。如果是,怎么做 提交帖子的代码: <div class="new-post"> <form action="" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="ti
<div class="new-post">
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title :</label><br />
<input class="form-control" type="text" id="title" value="" tabindex="1" size="20" name="title" minlength="5" required />
</div>
<div class="form-group">
<label for="description">Description :</label><br />
<textarea class="form-control" id="description" tabindex="3" name="description" cols="50" rows="6" minlength="30" required></textarea>
</div>
<div class="form-group">
<?php
$args_drop = array(
'show_option_none' => 'Category',
'hide_empty' => false,
'include' => '26, 27, 29, 34, 33',
'tab_index' => 3,
'taxonomy' => 'category',
);
wp_dropdown_categories($args_drop);
?>
</div>
<div class="form-group">
<label for="file">Image 2 :</label>
<input type="file" name="custom_1" value="Custom Field 1 Content" />
</div>
<div class="form-group">
<label for="postPhoto1">Photo 1 :</label>
<input type="file" name="upload_attachment[]" id="postPhoto1">
</div>
<div class="form-group">
<label for="postPhoto1">Photo 2 :</label>
<input type="file" name="upload_attachment[]" id="postPhoto2">
</div>
<input type="submit" value="Submit" tabindex="6" id="submit" name="submit" />
<input type="hidden" name="post_type" id="post_type" value="domande" />
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
}
现在的问题是,通过这段代码,我可以上传这些图片,但我应该把它们保存在哪里呢。就像一个特色图片在文章中有一个部分,在那里保存或者我如何将它们检索到一个页面
谢谢请提供更多信息:您做了什么,代码示例,预期和实际结果等。欢迎访问SO。请注意,“编写/调试我的代码”、“为我推荐/搜索”、“教程”请求和“低工作量”、“不清楚”、“基于意见”的问题。好的问题,如中所述,应进行研究,对问题进行清楚的解释,并应包括对未来访客有用的答案。请查看其是否清晰:-)
// Check if the form was submitted
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter the content';
}
$custom_field_1 = $_POST['custom_1'];
// Add the content of the form to $post as an array
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => array($_POST['cat']),
'post_status' => 'publish', // Choose: publish, preview, future, etc.
'post_type' => 'work' // Use a custom post type if you want to
);
$pid = wp_insert_post($post); // Pass the value of $post to WordPress the insert function
if ( $_FILES ) {
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
}
}
}
}
$category_filter = array($_POST['cat_filter']);