【问题标题】:Add function with custom parameter to wp footer将带有自定义参数的函数添加到 wp 页脚
【发布时间】: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


【解决方案1】:

首先,将您的 php 代码与 javascript 代码混合,创建一个 .js 文件并将其添加到页脚:http://codex.wordpress.org/Function_Reference/wp_register_script(将页脚设置为 true 以在页脚中包含您的 javacript 文件)确实是一种不好的做法; 然后为每个项目添加一个 css 类,然后在您的 javascript 文件中,您可以使用该 css 类引用所有项目。

【讨论】:

  • 我就是这样开始做的,除了每个标签是根据ID单独激活的。 ID 是在运行时使用 uniqid() 生成的。
  • 在你的 js 文件中你可以做 $(".nav-tabs a").each(function(){ $(this).tab("show"); });循环所有选项卡(具有唯一 ID)并将它们变成一个选项卡,虽然我不确定你使用的是什么 jquery 选项卡插件,例如在 twitter 引导选项卡中,选项卡不会像你一样启动正在做
  • 太棒了,做到了。谢谢你的帮助山姆。
猜你喜欢
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 2010-10-31
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多