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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题