【问题标题】:Users adding Images to a Slideshow through Posts in WordPress用户通过 WordPress 中的帖子将图像添加到幻灯片
【发布时间】:2012-10-28 12:17:06
【问题描述】:

由于 Stackexchange 的 WordPress 论坛几乎无人问津,我想我会在这里问同样的问题,因为这是我经常发帖的地方。

原帖:https://wordpress.stackexchange.com/questions/70742/users-adding-images-to-a-slideshow-through-posts

我对一个页面感到困惑,我希望用户能够在其中创建自己的帖子 - 为此我使用插件 WP User Frontend

每个帖子都包含各种文本字段,包括描述、位置、联系信息,最后是特色图片(缩略图)。

现在,我想添加一个功能,让我的用户可以将最多五张自选图片添加到幻灯片中——用户发布的每篇帖子都应该包含一个幻灯片。我该怎么做?

如果我可以让 WP 用户前端与现有的幻灯片插件(例如 Meteor SlidesSlideshow?)一起工作,我认为它可以工作吗?也许通过自定义 PHP 语句?

我已经尝试这样做好几天了。现在我求助于你,希望能最终解决这个问题。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    为了在外部上传图片,请将这些代码粘贴到您的functions.php中

    function uploadImage($new_post){
     $inc = 1;    
     if ($_FILES) {
       foreach ($_FILES as $file => $array) {          
          $newupload = insert_attachment($file,$new_post);
          // $newupload returns the attachment id of the file that
          // was just uploaded. Do whatever you want with that now.
          $inc++;
       }
     }
    
    }
    
    function insert_attachment($file_handler,$post_id) {
    
     // check to make sure its a successful upload
     if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) 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 );  
    }
    

    并使用您的新帖子 ID 调用模板中的 uploadimage 函数

    uploadImage($the_post_id);
    

    只需输入五个上传输入表单,它就会被函数自动读取

    【讨论】:

    • 我没有收到您帖子的通知 - 这就是我回复这么晚的原因。我想在标准single.php-模板中发布uploadImage($the_post_id);,这意味着,我希望自动选择$the_post_id - 你知道怎么做吗?
    • 如果你想获取 single.php 当前的帖子 ID,那么你可以这样做: global $post; uploadImage($post->ID);
    • <?php global $post; uploadImage($post->ID); ?> 没有显示帖子中所有上传的图片。图像仍然从<?php the_content(); ?> 显示。顺便说一句,我现在正在使用插件 Post from Site,因为 WP User Frontend 无法将超过 1 个图像附加到它的前端表单。
    • 你不能帮我解决这个问题吗,@loQ?
    猜你喜欢
    • 2016-08-15
    • 2012-05-04
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多