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

AdRock

我对Wordpress确实很陌生,并且慢慢地解决了这个问题,但是遇到了一个问题,我不知道如何解决。

我正在后端使用文本编辑器创建页面,并将html粘贴在其中。一切正常,我可以在functions.php中使用此功能输出图像(我在某处找到了此代码段,但不记得在哪里了)

// 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]

问题是我如何添加一些替代文本并向图像添加类

阿克谢·帕格达(Akshay Paghdar)

您可以添加shortcodecustom 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]

我希望这会有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章