首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用wordpress快捷方式添加html属性

使用wordpress快捷方式添加html属性
EN

Stack Overflow用户
提问于 2020-03-20 00:39:17
回答 1查看 170关注 0票数 1

我正在修改一个wordpress主题。我希望能够给出的选项,有一个链接打开在一个新的标签使用短码。

代码语言:javascript
复制
// add shortcode for About Our Charity Section
function theme_about_charity($atts, $content = null){
    extract(shortcode_atts(array(
        'title' => '',
        'icon' => '',
        'link' => '', 
        'last'  => '',
        'newTab' => ''
    ),$atts));
        return ' <div class="about-box" id="'.(($last == 'yes') ? 'last' : '').'">
                    <h3><i class="fa fa- '.$icon.'"></i><a href="'.$link.'" target="'.($newTab == 'yes')? '_blank' : '_self' . '">' .$title.'</a></h3>
                    <p>'.$content.'</p>                             
                </div>';
    }
add_shortcode('about_charity','theme_about_charity');

当用户创建短代码时:

代码语言:javascript
复制
[about_charity icon="icon" title="title" link="#" newTab="yes" ]lorem ipsum[/about_charity]

现在代码只是在我的网页上打印"_blank“。

代码用于修改div的id属性,但不能修改链接的目标属性。有没有人能解释一下这一点,并提供一个如何使其工作的建议?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-03-20 14:33:53

我们发现了两个错误

我们已经解决并更新了如下代码,请替换下面的函数以及一个短代码。

函数如下:

代码语言:javascript
复制
// add shortcode for About Our Charity Section
function theme_about_charity($atts, $content = null){
    extract(shortcode_atts(array(
        'title' => '',
        'icon' => '',
        'link' => '', 
        'last'  => '',
        'newtab' => ''
    ),$atts));
    return '<div class="about-box" id="'.(($last == 'yes') ? 'last' : '').'">
                <h3><i class="fa fa- '.$icon.'"></i><a href="'.$link.'" target="'.(($newtab == 'yes') ? '_blank' : '_self') . '">' .$title.'</a></h3>
                <p>'.$content.'</p>                             
            </div>';
}
add_shortcode('about_charity','theme_about_charity');

缩写如下:

代码语言:javascript
复制
[about_charity icon="icon" title="title1" link="https://www.google.com" newtab="yes" ]lorem ipsum[/about_charity]

我希望它能为你工作,因为我们已经尝试过了,它对我也是有效的。

谢谢!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60761448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档