【发布时间】:2014-02-26 16:39:42
【问题描述】:
我正在构建一个简单的短代码插件。其中一个简码需要在页脚中使用 jQuery 来激活具有 uniqid div id 的选项卡。我正在使用以下函数构建 jQuery:
function footer_scripts($id_array){
foreach($id_array as $id) {
$tab_loop .= '$(\'.nav-tabs a[href="#tabs-'.$id.'"]\').tab(\'show\');';
}
$return = 'jQuery(document).ready(function ($) {
// var touched = false;
e.preventDefault();';
$return .= $tab_loop;
$return .= '});';
return $return;
}
然后在构建选项卡的函数中调用该函数,如下所示:
function tab_group($atts, $content) {
extract(shortcode_atts(array(
// Option to use tabs or pills and open or closed framing
'nav' => 'tabs',
'style' => 'framed',
), $atts));
$GLOBALS['tab_count'] = 0;
do_shortcode($content);
if( is_array( $GLOBALS['tabs'] ) ){
// Create an array of IDs to pass to the footer js function
$id_array = array();
// Create arrays for the foreach loop to contain the tab selectors and contents. We will pull this apart outside of the loop using implode()
foreach( $GLOBALS['tabs'] as $tab ){
$id_array[] = $tab['id'];
$tabs[] = '<li class=""><a href="#tabs-'.$tab['id'].'" data-toggle="'.substr($nav,0,-1).'" title="'.$tab['title'].'">'.$tab['title'].'</a></li>';
$tab_contents[] = '<div id="tabs-'.$tab['id'].'"><h3>'.$tab['title'].'</h3>'.$tab['content'].'</div>';
}
footer_scripts($id_array);
$return = "\n".'<!-- Tabs Shortcode -->'."\n".'<ul class="nav nav-'.$nav.'">'.implode( "\n", $tabs ).'</ul>'."\n".'<!-- Tabs Content -->'."\n".'<div class="tab-content">'.implode( "\n", $tab_contents ).'</div>'."\n";
}
return $return;
}
所以我现在的问题是如何将这个新生成的代码块放入页脚。我知道我可以使用 add_action('wp_footer'... 调用函数,但它似乎不接受自定义参数。
我希望这是足够的信息!
谢谢!
【问题讨论】:
-
您走在正确的道路上,但是您需要更改您的函数,使其不接受参数,而是使用提供程序来提供选项。
-
感谢您的帮助。我似乎找不到任何有关如何设置它的资源,并且不太清楚您所说的“提供者”是什么意思。
-
基本上您必须在其他地方设置参数,例如数据库,然后您的函数可以调用可能提供所需参数的函数。例如,创建一个 get_widget_parameter(x_parameter) 函数,或者从 wordpress 配置中获取该函数所需的参数。
标签: php jquery wordpress plugins