【问题标题】:Add a rel nofollow to shortcode in wordpress在 wordpress 中将 rel nofollow 添加到简码
【发布时间】:2014-04-26 19:42:17
【问题描述】:

我为我的 WordPress 使用了一个按钮简码(见下文)。

如您所见,我可以选择颜色,放置链接,为目标属性指定一些内容。

我想将 rel nofollow 添加到这个 wordpress 按钮链接短代码,但我不知道该怎么做。

add_shortcode('button', 'shortcode_button');
function shortcode_button($atts, $content = null) {
    $atts = shortcode_atts(
        array(
            'color' => 'black',
            'link' => '#',
            'target' => '',
        ), $atts);

        return '[raw]<span class="button ' . $atts['color'] . '"><a href="' . $atts['link'] . '" target="' . $atts['target'] . '">' .do_shortcode($content). '</a></span>[/raw]';
}

谢谢

【问题讨论】:

  • 你想要那种动态的?
  • 不知道你说的动态是什么意思? (对不起,我是菜鸟)基本上,我将使用按钮短代码创建的所有下一个按钮链接都需要在 nofollow 中。
  • 好的,我已经发布了答案尝试一下。

标签: wordpress shortcode nofollow


【解决方案1】:

只需在您的链接中添加rel="nofollow"

add_shortcode('button', 'shortcode_button');
function shortcode_button($atts, $content = null) {
    $atts = shortcode_atts(
        array(
            'color' => 'black',
            'link' => '#',
            'target' => '',
        ), $atts);

        return '[raw]<span class="button ' . $atts['color'] . '"><a rel="nofollow" href="' . $atts['link'] . '" target="' . $atts['target'] . '">' .do_shortcode($content). '</a></span>[/raw]';
}

【讨论】:

  • 非常感谢!!有用。我会投票但没有足够的声誉对不起。
  • 别这样。很高兴帮助你:)
【解决方案2】:

使用下面的代码...你必须在你的这个函数中使用do_shortcode()。原因是您在其中使用了另一个短代码。

add_shortcode('button', 'shortcode_button');
function shortcode_button($atts, $content = null) {
    $atts = shortcode_atts(
        array(
            'color' => 'black',
            'link' => '#',
            'target' => '',
        ), $atts);

        return do_shortcode('[raw]<span class="button ' . $atts['color'] . '"><a href="' . $atts['link'] . '" target="' . $atts['target'] . '" rel="nofollow" >' .do_shortcode($content). '</a></span>[/raw]');
}

【讨论】:

  • 感谢您的回答。我没有尝试过你给我的代码,但我已经搜索了一些关于 do_shortcode() 的信息。看起来这对某些人来说可能很糟糕,我不知道在我的情况下......
猜你喜欢
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
  • 1970-01-01
  • 2011-06-29
相关资源
最近更新 更多