我使用一个ACF自定义类型的高级自定义重复器图片字段,以允许用户添加来自前端的帖子。我的更新代码运行得很好。插入新帖子不能正常工作。它只是添加了一个帖子,但没有保存ACF重复器图片字段的任何数据。
if (isset($_POST['submit'])) {
// 获取当前页面的链接
$current_url = get_page_link();
// 检查是否是一个POST请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES) && !empty($_FILES)) {
// 获取重复器字段的数据
$repeater_data = isset($_POST['acf']['hlj_works_img']) ? $_POST['acf']['hlj_works_img'] : array();
// 遍历重复器字段的每个子字段
foreach ($repeater_data as $index => $sub_field) {
// 假设每个子字段中有一个图片字段
if (isset($sub_field['hlj_work_picture_img'])) {
// 获取上传的图片信息
$uploaded_file = $_FILES['acf']['name']['hlj_work_picture_img_' . $index];
$tmp_name = $_FILES['acf']['tmp_name']['hlj_work_picture_img_' . $index];
// 处理图片上传
$attachment_id = acf_upload_file($tmp_name, $post_id, 'image');
// 如果上传成功,保存图片ID到自定义字段
if ($attachment_id) {
// 保存图片ID到文章的一个自定义字段中,比如使用数组格式存储所有图片ID
update_field('field_image_ids', array_merge((array) get_field('field_image_ids', $post_id), array($attachment_id)), $post_id);
// 或者为每个图片创建一个单独的自定义字段
update_field('field_image_' . $index, $attachment_id, $post_id);
}
}
// 处理其他子字段...
}
// 其他表单处理逻辑...
}
// 设置投稿数据
$post_data = array(
'post_type' => 'work', // 自定义文章类型
'post_title' => sanitize_text_field($_POST['post_title']),
'post_content' => wp_kses_post($_POST['post_content']),
'post_status' => 'pending', // 可以设置为 'draft', 'pending', 'publish' 等
'tax_input' => array(
'twork' => array($custom_taxonomy_id) // 将自定义分类法的ID作为数组传入
)
);
// 插入新的文章
$post_id = wp_insert_post($post_data);
update_post_meta($post_id,'_thumbnail_id',$attachment_id);
// 检查插入是否成功
if ($post_id) {
update_field('hlj_works_img', $hlj_works_img_v, $post_id); // 更新ACF字段
wp_die('提交成功!<a href="'.$current_url.'">点此返回</a>','提交成功');
} else {
echo "提交失败。";
};
};
复制
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<br><br>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>" enctype="multipart/form-data">
<label for="post_title">标题</label>
<input type="text" id="post_title" name="post_title" placeholder="请输入店铺名称" required /><br><br>
<label for="thumbnail">特色图片</label>
<input id="thumbnail" type="file" accept="image/*" name="thumbnail" /><br><br>
<!-- 重复器提交 -->
<div id="acf-repeater">
<!-- 遍历现有的重复项 -->
<?php $existing_data = get_field('hlj_works_img', $post_id);
if ($existing_data) {
foreach ($existing_data as $index => $row) {
?>
<div class="acf-repeater-row">
<input type="file" name="hlj_works_img[<?php echo $index; ?>][hlj_work_picture_img]" value="<?php echo esc_attr($row['hlj_work_picture_img']); ?>" id="hlj_work_picture_img" /><br><br>
<!-- 其他子字段 -->
</div>
<?php
}
};
?>
<!-- 添加新重复项的功能 -->
<button type="button" id="add-row">添加更多文本</button><br><br>
<script>
jQuery(document).ready(function($) {
$('#add-row').click(function() {
// 获取当前重复项的数量
var rowCount = $('.acf-repeater-row').length;
// 创建新的重复项表单行
var newRow = '<div class="acf-repeater-row">' +
'<input type="file" name="hlj_works_img[' + rowCount + '][hlj_work_picture_img]"><br><br>' +
'</div>';
// 将新的重复项添加到表单中
$('#acf-repeater').append(newRow);
});
});
</script>
</div><br><br>
<input type="submit" name="submit" value="提交"><br><br>
</form>
我不能弄清楚,我哪里弄错了。我希望当一个帖子被提交时,所有数据都被提交并保存,当更新时,所有数据都必须更新。
相似问题