doutang3077 2013-08-19 14:05
浏览 56
已采纳

在php函数中检测wordpress短代码中的参数名称?

I am trying yo understand this function, as a preface to forking it to make similar functions for my own shortcodes. I understand how to define shortcodes and their functions. I also basically "get" what the original author is doing here: collecting parameters from the shortcode and assembling them into an HTML tag and returning that tag. It seems the order of the params is unimportant, but their names are.

However, when I am working with this code, it does not seem to understand which param is which. For example, the original docs say to use the shortcode like so: [button link="http://google.com" color="black" size="small"]Button Text[/button]

But when I use this shortcode, I get:

<a href="Button Text" title="Array" class="button button-small button " target="_self">
  <span>Array</span>
</a>

Here's my PHP:

if( ! function_exists( 'make_button' ) ) {
function make_button( $text, $url, $color = 'default', $target = '_self', $size = 'small', $classes = null, $title = null ) {
    if( $target == 'lightbox' ) {
        $lightbox = ' rel="lightbox"';
        $target = null;
    } else {
        $lightbox = null;
        $target = ' target="'.$target.'"';
    }
    if( ! $title )
        $title = $text;
    $output = '<a href="'.$url.'" title="'.$title.'" class="button button-'.$size.' '.$color.' '.$classes.'"'.$target.$lightbox.'>';
    $output .= '<span>'.$text.'</span>';
    $output .= '</a>';
    return $output;
}
}


add_shortcode( 'button', 'make_button' );
  • 写回答

2条回答 默认 最新

  • doucan4873 2013-08-19 14:31
    关注

    See the documentation for Shortcode API, there clearly states that three parameters are passed to the shortcode callback function:

    • $atts - an associative array of attributes, or an empty string if no attributes are given
    • $content - the enclosed content (if the shortcode is used in its enclosing form)
    • $tag - the shortcode tag, useful for shared callback functions

    So the function definition should look like:

    function make_button( $atts, $content, $tag ) {
        // use print_r to examine attributes
        print_r($atts);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器