【问题标题】:Using wordpress shortcode to add html attributes使用 wordpress 短代码添加 html 属性
【发布时间】:2020-06-30 20:09:53
【问题描述】:

我正在修改一个 wordpress 主题。我希望能够提供使用简码在新选项卡中打开链接的选项。

// 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');

当用户创建简码时:

[about_charity icon="icon" title="title" link="#" newTab="yes" ]lorem ipsum[/about_charity]

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

该代码用于修改 div 的 id 属性,但不能修改链接的 target 属性。有人可以解释这一点并就如何使这项工作提出建议吗?谢谢!

【问题讨论】:

    标签: php html wordpress shortcode


    【解决方案1】:

    我们发现了两个错误

    我们已经解决并更新了以下代码请替换以下函数以及简码。

    功能如下:

    // 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');

    简码如下:

    [about_charity icon="icon" title="title1" link="https://www.google.com" newtab="yes" ]lorem ipsum[/about_charity]

    我希望它对你有用,因为我们已经尝试过,它对我有用。

    谢谢!

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多