2017-04-27 64 views
0

这是我的短代码:简码始终显示默认参数

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/

+0

解决:不能在参数名称大写字母。大声笑 – michaelnewsomejr

回答

1

短代码属性在传递之前转换为小写到你的回拨。

您的简码中的sectionHeading属性将为$atts阵列中的sectionheading

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

通常你会看到用来替代骆驼案例简码属性下划线。

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

使用

[sectionWithSidebar section_heading="Global Health" . . .