【问题标题】:Wordpress: Function to add images in text editorWordpress:在文本编辑器中添加图像的功能
【发布时间】:2014-06-06 14:04:59
【问题描述】:

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

我正在使用后端的文本编辑器创建页面,并在其中粘贴 html。一切正常,我可以在functions.php中使用这个函数输出图像(我发现这个sn-p 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]

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

【问题讨论】:

    标签: wordpress image function templates


    【解决方案1】:

    您可以像这样添加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]
    

    我希望这会有所帮助。

    【讨论】:

    • 绝对正确。正是我想要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多