我正在尝试实现首页Wordpress上载程序,它使用户可以从Wordpress页面上载图像,并且还可以在上载前调整图像的大小。我找到了Agile Uploader。上传者为表格。

问题是,当我单击表单中的“提交”按钮以发送数据时,所有字段都存储在帖子中,而图像没有存储。

这是我的上传页面的代码:

<form id="submitForm" action="<?php echo get_permalink(); ?>" method="post" enctype="multipart/form-data" onsubmit="return ray.ajax()">
<!-- upload photos -->

  <div style="float:left;width:410px; height:246px;">
    <div id="multiple"></div>
  </div>

  <script type="text/javascript">
    jQuery('#multiple').agileUploader({
      formId: 'submitForm',
      flashVars: {
        file_post_var: 'attachment',
        firebug: false,
        form_action: '',
        file_limit: 15,
        max_post_size: (1000 * 1024)
      }
    });
  </script>

  </div>   <!-- end - upload photos -->
</form>

以及Wordpress上传的代码(在同一文件中)
/* upload photos */
if ($post_error == false) {

  /* required files */
  require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  require_once(ABSPATH . "wp-admin" . '/includes/media.php');

  $files = $_FILES['attachment'];

  if ($files) {

    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("attachment" => $file);
      //$_FILES = array_reverse($_FILES);
      foreach ($_FILES as $file => $array) {
        $attach_id = media_handle_upload( $file, $ad_id, array(), array( 'test_form' => false ) );
        if ($attach_id < 0) { $post_error = true;
      }
    }
  }
}

我究竟做错了什么?

最佳答案

您确定图像未保存为所创建帖子的“附件”吗?

尝试运行:

$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude'     => get_post_thumbnail_id())
);
var_dump($attachments);

在用于查看帖子的模板文件中。
如果您对编码不满意,则可以使用插件来显示附件。
像这个"List Attachments Shortcode"插件。

10-06 13:22
查看更多