我试图分配用户为每个问题上传的图片,并且应该与每个问题一起显示。我正在使用' media_handle_upload' WordPress功能进行上传。
<div id="submit-question" class="dwqa-submit-question">
<?php
global $dwqa_options, $dwqa_current_error;
if( is_wp_error( $dwqa_current_error ) ) {
$error_messages = $dwqa_current_error->get_error_messages();
if( !empty($error_messages) ) {
echo '<div class="alert alert-error">';
foreach ($error_messages as $message) {
echo $message;
}
echo '</div>';
}
}
?>
<form action="" name="dwqa-submit-question-form" id="dwqa-submit-question-form" method="post">
<div class="question-advance">
<div class="question-meta">
<div class="select-category">
<label for="question-category"><?php _e('Question Category','dwqa') ?></label>
<?php
wp_dropdown_categories( array(
'name' => 'question-category',
'id' => 'question-category',
'taxonomy' => 'dwqa-question_category',
'show_option_none' => __('Select question category','dwqa'),
'hide_empty' => 0,
'quicktags' => array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close' ),
'selected' => (isset( $_POST['question-category'] ) ? stripslashes(htmlentities($_POST['question-category'])) : false)
) );
?>
</div>
<div class="input-tag">
<label for="question-tag"><?php _e('Question Tags','dwqa') ?></label>
<input type="text" name="question-tag" id="question-tag" placeholder="<?php _e('tag 1, tag 2,...','dwqa') ?>" value="<?php echo isset( $_POST['question-tag'] ) ? stripslashes(htmlentities($_POST['question-tag'])) : ''; ?>" />
</div>
</div>
</div>
<div class="input-title">
<label for="question-title"><?php _e('Your question','dwqa') ?> *</label>
<input type="text" name="question-title" id="question-title" placeholder="<?php _e('How to...','dwqa') ?>" autocomplete="off" data-nonce="<?php echo wp_create_nonce( '_dwqa_filter_nonce' ) ?>" value="<?php echo isset( $_POST['question-title'] ) ? stripslashes(htmlentities($_POST['question-title'])) : ''; ?>" />
<span class="dwqa-search-loading dwqa-hide"></span>
<span class="dwqa-search-clear fa fa-times dwqa-hide"></span>
</div>
<div class="question-advance">
<div class="input-content">
<label for="question-content"><?php _e('Question details','dwqa') ?></label>
<?php
dwqa_init_tinymce_editor( array(
'content' => ( isset( $_POST['question-content'] ) ? stripslashes(htmlentities($_POST['question-content'])) : '' ),
'id' => 'dwqa-question-content-editor',
'textarea_name' => 'question-content',
'media_buttons' => true
) );
?>
</div>
<?php if( isset($dwqa_options['enable-private-question']) && $dwqa_options['enable-private-question'] ) : ?>
<div class="checkbox-private">
<label for="private-message"><input type="checkbox" name="private-message" id="private-message" value="true"> <?php _e('Post this Question as Private.','dwqa') ?> <i class="fa fa-question-circle" title="<?php _e('Only you as Author and Admin can see the question', 'dwqa') ?>"></i></label>
</div>
<?php endif; ?>
<div class="question-signin">
<?php do_action( 'dwqa_submit_question_ui' ); ?>
</div>
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'clean'
};
</script>
<?php
global $dwqa_general_settings;
if( dwqa_is_captcha_enable_in_submit_question() ) {
$public_key = isset($dwqa_general_settings['captcha-google-public-key']) ? $dwqa_general_settings['captcha-google-public-key'] : '';
echo '<div class="google-recaptcha">';
echo recaptcha_get_html($public_key);
echo '<br></div>';
}
?>
</div>
<div class="form-submit">
<input type="submit" value="<?php _e('Ask Question','dwqa','Upload') ?>" class="dwqa-btn dwqa-btn-success btn-submit-question" name="submit_my_image_upload"/>
</div>
</form>
我的代码从下面开始:
<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
<input type="file" name="my_image_upload" id="my_image_upload" multiple="false" />
<input type="hidden" name="post_id" id="post_id" value="55" />
<?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
<input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>
<?php
if (
isset( $_POST['my_image_upload_nonce'], $_POST['question-category'] )
&& wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
) {
// The nonce was valid and the user has the capabilities, it is safe to continue.
// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// Let WordPress handle the upload.
// Remember, 'my_image_upload' is the name of our file input in our form above.
$attachment_id = media_handle_upload( 'my_image_upload', $_POST['question-category'] );
if ( is_wp_error( $attachment_id ) ) {
// There was an error uploading the image.
echo 'Error uploading';
} else {
// The image was uploaded successfully!
}
} else {
// The security check failed, maybe show the user an error.
}?>
</div>
这是插件的问题 - submitt-form.php,我非常确定我应该实现&#39; media_handle_upload&#39;表单内部的功能,以便每个上传的图片都会得到问题的ID,并且按下提交图片将与问题相关,但无法使其正常工作。如果它超出了第一个&#34;形式&#34;它上传图片但与问题无关。
PS:如果有另一种解决方案可以实现这一点(Q&amp; A Reddit,Stack Overflow风格和图像分配给我打开建议的每个问题)。
答案 0 :(得分:0)
我在任务方面取得了一些进展:添加:
add_post_type_support('dwqa-question', array('thumbnail'));
add_theme_support('post-thumbnails');
在function.php中的让我从前端显示后端的特色图像:
<div class="topic_image"><?php the_post_thumbnail('thumbnail');?></div>
现在问题是如何从前端分配特色图像。我想出了下面看起来合乎逻辑但不起作用的代码
<input type="file" name="imageFeatured" id="imageFeeatured"/>
<?php
if ( ! empty( $_POST[ 'post-thumbnails' ] ) )
$thumbnail_field = esc_html( $_POST[ 'post-thumbnails' ] );
else
$thumbnail_field = 'imageFeatured';
if ( ! empty( $_FILES ) ) {
foreach ( $_FILES as $file => $array )
$newupload = insert_attachment( $file, $pid, $thumbnail_field );
}
//attachment helper function
function insert_attachment( $file_handler, $post_id, $set_thumb = false ) {
if ( UPLOAD_ERR_OK !== $_FILES[ $file_handler ]['error'] )
return false;
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$attach_id = media_handle_upload( $file_handler, $post_id );
//set post thumbnail (featured)
if ( $attach_id && $set_thumb )
update_post_meta( $post_id, 'thumbnail', $attach_id );
return $attach_id;
}
?>