短代码始终显示默认参数

时间:2017-04-27 02:05:27

标签: wordpress shortcode

这是我的短代码:

function sectWithSidebar($atts, $content, $tag){
    //collect values, combining passed in values and defaults
    $values = shortcode_atts(array(
        'sectionHeading' => 'Section Heading',
        'sidebarHeading' => 'Sidebar Heading',
        'sidebarText' => 'Sidebar Text',
        'buttonURL' => "#",
        'classes' => ''
    ),$atts); 

    $output = '<div class="container section section-with-sidebar'.$values["classes"].'"><div class="row"><div class="col-sm-12"><h2>'.$values["sectionHeading"].'</h2></div></div><div class="row"><div class="col-md-8 col-sm-12"><span>'.$content.'</span></div><div class="col-md-4 col-sm-12"><div class="sidebar"><div class="sidebar-header">'.$values["sidebarHeading"].'</div><div class="sidebar-content">'.$values["sidebarText"].'<span class="learn-more-button"><a href="'.$values["buttonURL"].'">Learn More</a></span></div></div></div></div></div>';
    return $output;
}
add_shortcode('sectionWithSidebar','sectWithSidebar');

我试图像这样显示它:

[sectionWithSidebar sectionHeading="Global Health" sidebarHeading="SidebarHeading" sidebarText="SidebarText" buttonURL="http://www.google.com"]
MSIH is a unique school that prepares physicians to address the impact of cultural, economic, political and environmental factors on the health of individuals and populations worldwide. It is the world’s first and only medical school to incorporate core global health courses into all four years of an M.D. program. Classes are small, intimate and challenging. At MSIH, you learn to be not only a doctor, but a skilled physician with a comprehensive view of health around the world.
[/sectionWithSidebar]

但是,它只显示默认内容,就好像我在向页面添加短代码时没有定义任何参数: http://109.199.101.20/~newmsih/template-1-with-shortcodes/

1 个答案:

答案 0 :(得分:1)

短代码属性在传入回调之前会转换为小写。

短代码的sectionHeading属性在sectionheading数组中为$atts

https://codex.wordpress.org/Function_Reference/add_shortcode

通常情况下,您会看到使用下划线代替驼峰大小写的短代码属性。

$values = shortcode_atts(array(
    'section_heading' => 'Section Heading',
    . . .

用法

[sectionWithSidebar section_heading="Global Health" . . .