具有自定义样式的按钮的短代码

时间:2014-03-18 09:21:24

标签: wordpress shortcode

我已在我的小部件中添加了一个链接 例如:

 <a target="_blank" href="google.com" class="a-button">Learn More</a>

我需要像这样的短代码

[button link="google.com" value="Learn More"]

如果我将此短代码粘贴到小部件,页面和帖子中,则必须显示链接

样式必须与上面的标记相同

当前代码:

function button_shortcode($atts, $content = null) {
    extract( shortcode_atts( array( 'url' => '#' ), $atts ) );
    return '<a href="'.$url.'" class="a-button">' . do_shortcode($content) . '</a>';
}
add_shortcode('button', 'button_shortcode');

我该怎么做?

3 个答案:

答案 0 :(得分:1)

基本短代码如下所示:

function a_button_shortcode( $atts, $content = null ) {
   extract($atts);
   return '<a target="_blank" href="' . esc_attr($link) . '" class="a-button">' . esc_attr($value) .   '</a>';
}
add_shortcode( 'button', 'a_button_shortcode' );

您可以在http://codex.wordpress.org/Shortcode_API

上阅读有关短代码API的更多信息

为了使您的小部件具有短代码,请在小部件的更新方法中使用do_shortcode($ content)函数。

像这样:

function update( $old_instance, $new_instance) {
   $new_instance['content'] = do_shortcode($new_instance['content']);
   return $new_instance;
}

或使用一个插件,使其成为默认小部件,例如https://wordpress.org/plugins/shortcodes-in-sidebar-widgets/

答案 1 :(得分:0)

add_shortcode("init" ,"add_custom_shortcode");

function add_custom_shortcode()
{
     add_shortcode('button', 'buttonShortcode');
}

function buttonShortcode($atts, $text='') {
    extract( shortcode_atts( array( 'url' => '#' ), $atts ) );
    return '<a href="'.$url.'" class="a-button">' . do_shortcode($text) . '</a>';
}

答案 2 :(得分:0)

谢谢大家,我试过这个

function button_shortcode($atts, $content = null) {
 extract( shortcode_atts( array(
          'url' => '#'
), $atts ) );
return '<a href="'.$url.'" class="a-button">' . do_shortcode($content) . '</a>';
}
add_shortcode('button', 'button_shortcode');\

对于functions.php

中下面的小部件粘贴中的短代码支持
add_filter('widget_text', 'do_shortcode');

创建短代码:

[button url="google.com"]Download[/button]