【问题标题】:How to call an image by WordPress shortcode attribute?如何通过 WordPress 短代码属性调用图像?
【发布时间】:2014-03-21 15:06:44
【问题描述】:

我正在为 wordpress.org 开发一个 WordPress 插件,这是我的第一个插件。有一个我自己无法解决的问题(我做了很多谷歌但仍然找不到任何解决方案)。但也许这个问题的解决方案对你们中的一些人来说非常简单。因此,如果您知道直接的解决方案,请回答;我将不胜感激。

我的插件允许用户为每列制作带有横幅图像的表格。请检查此demo link

每列的标题部分有两个部分。一个是subject,另一个是image。简码看起来像

[head subject="Standard" image="http://shahriarblog.com/wp-content/uploads/2014/03/hello-5.jpg "][/head]

上述短代码的源代码如下:

function head($atts, $content = null) {
    extract( shortcode_atts( array(
        'color' => '#333',
        'banner' => 'visible',
        'subject' => 'Subject',
        'image' => 'http://shahriarblog.com/wp-content/uploads/2014/03/hello-5.jpg',
    ), $atts ) );
    return "<div class='gridacc'><h2 style='background:{$color}'>{$subject}</h2><div class='photo' style='display: {$banner}ne'><img src='{$image}' alt=''></div><dl>".do_shortcode($content)."</dl></div>";
}
add_shortcode ("head", "head");

如您所见,用户必须知道图像的 url 才能在短代码中使用它们。但我不想使用图片网址,而是只使用图片名称。例如,我想使用 image="hello-5.jpg" 而不是使用 image="http://shahriarblog.com/wp-content/uploads/2014 /03/hello-5.jpg"

另一件事: 现在要获取图片的 url,每个用户都需要通过 WordPress Media 上传图片,然后从媒体库中获取每张图片的 url。但是是否可以直接在我的插件文件夹而不是默认的 WordPress Uploads 文件夹中上传图像?

这里是 link 该插件的文档和source code,如果您有兴趣。非常感谢。

【问题讨论】:

    标签: image wordpress shortcode


    【解决方案1】:

    我认为最直接的方法是直接查询数据库,在wp_postmeta 表中查找附件hello-5.jpg 以获取其ID。从那里,使用regular WP function 拉取 URL:

    function head($atts, $content = null) {
        extract( shortcode_atts( array(
            'color' => '#333',
            'banner' => 'visible',
            'subject' => 'Subject',
            'image' => 'http://example.com/default_image.jpg',
        ), $atts ) );
    
        // The attribute image was passed in the shortcode [head image="hello-5.jpg"]
        if( $image != 'http://example.com/default_image.jpg' ) {
            global $wpdb;
            $attachment_id = $wpdb->get_var(
                        $wpdb->prepare( "
                            SELECT post_id
                            FROM $wpdb->postmeta
                            WHERE meta_value LIKE %s
                            AND meta_key = '_wp_attached_file'
                        ", "%{$image}%" 
                        )
                    );
            // Found it, change the $image URL
            if( $attachment_id )
                $image = wp_get_attachment_image_src( $attachment_id, 'full' );
        }
    
        return "<div class='gridacc'>
            <h2 style='background:{$color}'>{$subject}</h2>
        <div class='photo' style='display: {$banner}ne'>
            <img src='{$image}' alt=''>
        </div>
        <dl>".do_shortcode($content)."</dl>
        </div>";
    }
    

    以下问答展示了如何使用 Ajax 来改进 Shortcode TinyMCE 按钮的界面:
    Call PHP function inside TinyMCE modal window on the WordPress backend

    【讨论】:

    • "我觉得最直接的就是直接查询数据库,找附件hello-5.jpg" 如果每次都找hello-5.jpg 会怎样?你知道每次都会有每个新图像的新名称。所以我们不能简单地把 $search = 'hello-5.jpg';对吗?
    • @Shahriar,我已经修改了我的示例代码以使用您的函数。
    • 非常感谢您的帮助。当我应用您的代码时显示错误,致命错误:在非对象上调用成员函数 get_var()。有时间的话,请看我这里的源代码shahriarblog.com/ratcat-table-accordion.zip
    • @Shahriar,ups,我在混合代码时丢失了一行,请检查更新。
    • 虽然我还没有检查你的解决方案,因为我得到了另一个解决方案。但我真的很感谢你的帮助。谢谢。
    猜你喜欢
    • 2020-06-30
    • 2023-04-05
    • 1970-01-01
    • 2013-10-18
    • 2015-12-23
    • 2014-01-04
    • 1970-01-01
    • 2012-03-16
    • 2016-06-22
    相关资源
    最近更新 更多