在我开始之前,我很抱歉,如果我的英语不好,我不是母语,这是我在网站上的第一个问题。 我正在使用wordpress与我从头开始制作的自定义模板,我不想使用外部插件。
我有一个前端表单,允许用户上传当前正在运行的帖子。
我添加了一个选项来上传图片并将其设置为缩略图,这也可以。
现在我想添加上传图片的选项,并将它们附加到帖子内容,这样我以后就可以用single.php中的图片和内容显示帖子
这就是html现在的样子:
<?php new_post(); ?>
<form class="col-sm-12 col-md-5" id="new_post" name="new_post" enctype="multipart/form-data" method="post" action="">
<p>Título de la publicación:</p>
<input type="text" id="titulo" value="" size="20" name="titulo" />
<p>Imagen principal de la publicación:</p>
<input class="selector-archivo" type="file" name="imagen-post" id="imagen-post" >
<p>Agregar imágenes a la publicación:</p>
<input class="selector-archivo" type="file" name="fotosVehiculo[]" id="fotosVehiculo[]" multiple />
<div id="upload-image-container"></div>
<p>Tipo de vehículo:
<select name="select_vehiculo" class="listado-filtro">
<option disabled selected value>Elija un vehículo...</option>
<?php
$postTypes = get_post_types('', 'objects');
foreach ($postTypes as $postType) {
if ($postType->labels->es_vehiculo == '1') {
$name = $postType->labels->name;
echo '<option>' . $name . '</option>';
}
}
?>
</select>
</p>
<p>Marca:
<select name="select_marca" class="listado-filtro">
<option disabled selected value>Elija una marca...</option>
<?php
$taxonomy = array('taxonomy' => 'marcas_y_modelos');
$tax_terms = get_terms($taxonomy, array('hide_empty' => false));
foreach ($tax_terms as $tax_term) {
$term = $tax_term->name;
echo '<option value="' . $term . '">' . $term . '</option>';
}
?>
</select>
</p>
<p>Descripción: </p>
<textarea id="descripcion" name="descripcion" cols="50" rows="6"></textarea>
<p align="right"><input type="submit" value="Publicar" id="publicar" name="publicar" />
</p>
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post', 'nonce-checker'); ?>
</form>
这是处理所有事情的函数:
function new_post() {
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
if ( !isset( $_POST['nonce-checker'] ) || !wp_verify_nonce( $_POST['nonce-checker'], 'new-post' ) ) {
wp_safe_redirect( home_url() );
exit;
} else {
//validations...
if (isset ($_POST['titulo'])) {
$title = trim($_POST['titulo']);
} else {
echo 'Por favor inserte un título';
}
if (isset ($_POST['descripcion'])) {
$description = trim($_POST['descripcion']);
} else {
echo 'Por favor inserte el contenido';
}
if (isset ($_POST['select_vehiculo'])) {
$vehiculo = $_POST['select_vehiculo'];
$vehiculo = sanitizeFiltro($vehiculo);
} else {
echo 'Por favor elija un tipo de vehículo';
}
if (isset ($_POST['select_marca'])) {
$marca = $_POST['select_marca'];
$marca = sanitizeFiltro($marca);
} else {
echo 'Por favor elija una marca';
}
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => array('marcas_y_modelos'),
'tax_input' => array('marcas_y_modelos' => $marca ),
'post_status' => 'publish',
'post_type' => $vehiculo,
);
$post_id = wp_insert_post($post);
wp_set_object_terms($post_id, $marca, 'marcas_y_modelos', true);
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $post_id );
if ($file['imagen-post']) {
update_post_meta($post_id,'_thumbnail_id',$attach_id);
}
}
} else {
echo '<hr><p style="color:red">No se cargaron imágenes</p><hr>';
}
wp_safe_redirect( get_permalink( get_page_by_title( 'Alta') ) );
exit;
}
}
}
问题在于图像根本没有被上传,我无法理解为什么,我已经尝试了很多可能的搜索但没有工作。请我需要一个经过测试的解决方案
答案 0 :(得分:0)
我不了解foreach语法。如戴夫mentions in the comments:
哦,是的,我没听懂,您可能会误解foreach语法。它应该是
foreach (array_expression as $key => $value)
。创建它时,$file
变量将仅保留字符串键,而$array
将保留您实际尝试使用的值。 https://secure.php.net/manual/en/control-structures.foreach.php