我有一个使用自定义短代码的现有站点。当我尝试将它们转换为新主题时&;dev站点,它给了我一个错误。快捷码是关于选项卡式内容的。您可以在此处看到错误:https://dev.brothermartin.com/extracurriculars/academic-games/ 就是这样:
注意:未定义变量:tab3\\u button in/home/brothermartin18/public\\u html/blog/wp content/themes/Brother\\u Martin\\u 2018/lib/admin/shortcodes/shortcodes。php第60行
注意:未定义变量:tab4\\u button in/home/brothermartin18/public\\u html/blog/wp content/themes/Brother\\u Martin\\u 2018/lib/admin/shortcodes/shortcodes。php第60行
注意:未定义变量:tab5\\u button in/home/brothermartin18/public\\u html/blog/wp content/themes/Brother\\u Martin\\u 2018/lib/admin/shortcodes/shortcodes。php第60行
这是与短代码相关的代码:
function mbiz_tabbed_box( $atts, $content = null ) {
extract( shortcode_atts( array(
\'tab1\' => \'\',
\'tab2\' => \'\',
\'tab3\' => \'\',
\'tab4\' => \'\',
\'tab5\' => \'\',
), $atts ) );
if ($tab1) $tab1_button = \'<li><a href="#" title="tab1">\' . esc_attr($tab1) . \'</a></li>\';
if ($tab2) $tab2_button = \'<li><a href="#" title="tab2">\' . esc_attr($tab2) . \'</a></li>\';
if ($tab3) $tab3_button = \'<li><a href="#" title="tab3">\' . esc_attr($tab3) . \'</a></li>\';
if ($tab4) $tab4_button = \'<li><a href="#" title="tab4">\' . esc_attr($tab4) . \'</a></li>\';
if ($tab5) $tab5_button = \'<li><a href="#" title="tab5">\' . esc_attr($tab5) . \'</a></li>\';
return \'<div class="tabs-style1"><div class="tabs-navigation"><ul class="tabs-nav">\'
. $tab1_button . $tab2_button . $tab3_button . $tab4_button . $tab5_button .
\'</ul></div><div class="tabs-content">\' . do_shortcode($content) . \'</div></div>\';
}
add_shortcode(\'tabbed_box\', \'mbiz_tabbed_box\');
有人对此有任何简单的修复方法吗?仅当未使用5个选项卡时才会发生。我没有创建这些,所以很难排除故障。
最合适的回答,由SO网友:Ciprian 整理而成
首先初始化变量。请参见下面的代码:
function mbiz_tabbed_box( $atts, $content = null ) {
extract( shortcode_atts( array(
\'tab1\' => \'\',
\'tab2\' => \'\',
\'tab3\' => \'\',
\'tab4\' => \'\',
\'tab5\' => \'\',
), $atts ) );
$tab1_button = $tab2_button = $tab3_button = $tab4_button = $tab5_button = \'\';
if ($tab1) $tab1_button = \'<li><a href="#" title="tab1">\' . esc_attr($tab1) . \'</a></li>\';
if ($tab2) $tab2_button = \'<li><a href="#" title="tab2">\' . esc_attr($tab2) . \'</a></li>\';
if ($tab3) $tab3_button = \'<li><a href="#" title="tab3">\' . esc_attr($tab3) . \'</a></li>\';
if ($tab4) $tab4_button = \'<li><a href="#" title="tab4">\' . esc_attr($tab4) . \'</a></li>\';
if ($tab5) $tab5_button = \'<li><a href="#" title="tab5">\' . esc_attr($tab5) . \'</a></li>\';
return \'<div class="tabs-style1"><div class="tabs-navigation"><ul class="tabs-nav">\'
. $tab1_button . $tab2_button . $tab3_button . $tab4_button . $tab5_button .
\'</ul></div><div class="tabs-content">\' . do_shortcode($content) . \'</div></div>\';
}
add_shortcode(\'tabbed_box\', \'mbiz_tabbed_box\');
另一种解决方案是构建一个字符串,然后返回它,而不使用单独的变量。
$out = \'<div class="tabs-style1"><div class="tabs-navigation"><ul class="tabs-nav">\';
if ($tab1) $out .= \'<li>whatever</li>\';
if ($tab2) $out .= \'<li>whatever</li>\';
if ($tab3) $out .= \'<li>whatever</li>\';
if ($tab4) $out .= \'<li>whatever</li>\';
if ($tab5) $out .= \'<li>whatever</li>\';
$out .= \'</ul></div><div class="tabs-content">\' . do_shortcode($content) . \'</div></div>\';
return $out;