无法在前端显示上传的图片(从自定义按钮上传

时间:2011-05-09 作者:janoChen

我跟着this 教程在Wordpress管理中添加自定义上传按钮。

保存后立即显示您刚上传的图片:

enter image description here

问题是我无法将图片显示在前端的模板文件中。

这是显示图片的部分:

   function ud_section_text() {
        $options = get_option(\'ud_options\');
        echo \'<p>Upload your file here:</p>\';
        if ($file = $options[\'file\']) {
            // var_dump($file);
            echo "<img src=\'{$file[\'url\']}\' />";
        }
    }
所以我尝试了这个:

<?php var_dump($file[\'url\']); ?> 

but it returns NULL.

有什么建议吗?

代码:

<?php
/*
Plugin Name: Upload Demo
Description: Demonstrate a plugin that lets you upload an image
Author: Otto
Author URI: http://ottodestruct.com
License: GPL2

    Copyright 2010  Samuel Wood  (email : [email protected])

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2,
    as published by the Free Software Foundation.

    You may NOT assume that you can use any other version of the GPL.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    The license for this software can likely be found here:
    http://www.gnu.org/licenses/gpl-2.0.html

*/

// add the admin page and such
add_action(\'admin_init\', \'ud_admin_init\');
function ud_admin_init() {
    register_setting( \'ud_options\', \'ud_options\', \'ud_options_validate\' );
    add_settings_section(\'ud_main\', \'Main Section\', \'ud_section_text\', \'ud\');
    add_settings_field(\'ud_filename\', \'File:\', \'ud_setting_filename\', \'ud\', \'ud_main\');
}

// add the admin options page
add_action(\'admin_menu\', \'ud_admin_add_page\');
function ud_admin_add_page() {
    $mypage = add_options_page(\'Upload Demo\', \'Upload Demo\', \'manage_options\', \'ud\', \'ud_options_page\');
}

// display the admin options page
function ud_options_page() {
?>
    <div class="wrap">
    <h2>Upload Demo</h2>
    <p>You can upload a file. It\'ll go in the uploads directory.</p>
    <form method="post" enctype="multipart/form-data" action="options.php">
    <?php settings_fields(\'ud_options\'); ?>
    <?php do_settings_sections(\'ud\'); ?>
    <p class="submit">
    <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e(\'Save Changes\') ?>" />
    </p>
    </form>

    </div>

<?php
}

function ud_section_text() {
    $options = get_option(\'ud_options\');
    echo \'<p>Upload your file here:</p>\';
    if ($file = $options[\'file\']) {
        // var_dump($file);
        echo "<img src=\'{$file[\'url\']}\' />";
    }
}

function ud_setting_filename() {
    echo \'<input type="file" name="ud_filename" size="40" />\';
}

function ud_options_validate($input) {
    $newinput = array();
    if ($_FILES[\'ud_filename\']) {
        $overrides = array(\'test_form\' => false);
        $file = wp_handle_upload($_FILES[\'ud_filename\'], $overrides);
        $newinput[\'file\'] = $file;
    }
    return $newinput;
}

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

对此的简单回答是,如果要显示图像,请使用:

 $options = get_option(\'ud_options\');
 if (isset($options[\'file\'])) {
    echo "<img src=\'{$options[\'file\'][\'url\']}\' />";
 }

结束

相关推荐

Resizing all images

我刚刚更改了博客的主题,由于页面宽度很小,它打破了布局。我之前的大多数图片(附在帖子中)的宽度都在600px左右,现在我需要按450px的比例调整它们的大小。如何一次性调整它们的大小?有这个插件吗?P、 美国:编写CSS规则是一个很好的选择,我已经做到了。但我认为调整图像大小也会减少每个页面的下载开销!