Wordpress:在文本编辑器中添加图像的功能

时间:2014-04-22 09:24:43

标签: wordpress image function templates

我是Wordpress的新手,慢慢地了解它,但我遇到了一个问题,我不知道如何修复它。

我正在使用后端的文本编辑器创建页面,我在那里粘贴html。一切运作良好,我可以在functions.php中使用此功能输出图像(我发现这个片段在somwhere但不记得在哪里)

// Create the shortcode
function template_url( $atts, $content = null ) {
    return '<img src="'. get_template_directory_uri() .'/'. $content .'" alt="" />';
}  

// Add as shortcode
add_shortcode("template", "template_url");

我称之为

[template]images/my-image.jpg[/template]

问题是如何添加一些替代文字并为图像添加一个类

1 个答案:

答案 0 :(得分:1)

您可以使用shortcode添加custom attributes,如下所示: -

添加以下用于添加短代码的代码:

function template_url( $atts, $content = null )
{
    extract(
        shortcode_atts( array(
            'alt' => '',
            'width' => '',
            'height' => ''
        ), $atts )
    );

    //And this variable you can use like this:-
    return '<img src="'. get_template_directory_uri() .'/'. $content .'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"/>';
}

add_shortcode("template", "template_url");

在编辑器中使用此短代码: -

[template alt="this is alter text" width="400" height="300"]images/my-image.jpg[/template]

我希望这会有所帮助。