创建jQuery选项卡短代码

时间:2013-07-25 11:02:52

标签: jquery wordpress tabs shortcode

我正在尝试为WordPress创建一个jQuery标签短代码这是我的functions.php中的代码,因为某些原因标签显示等但它们没有改变?

add_shortcode('tabs', 'tabs_group');
add_shortcode('tab', 'tab');

// this variable will hold your divs
$tabs_divs = '';

function tabs_group( $atts, $content = null ) {
    global $tabs_divs;

    // reset divs
    $tabs_divs = '';

    extract(shortcode_atts(array(  
        'id' => '',
        'class' => ''
    ), $atts));  

    $output = '<ul class="nav nav-tabs '.$class.'"  ';

    if(!empty($id))
        $output .= 'id="'.$id.'"';

    $output.='>'.do_shortcode($content).'</ul>';
    $output.= '<div class="tab-content">'.$tabs_divs.'</div>';

    return $output;  
}  


function tab($atts, $content = null) {  
    global $tabs_divs;

    extract(shortcode_atts(array(  
        'id' => '',
        'title' => '',
        'active'=>'n' 
    ), $atts));  

    if(empty($id))
        $id = 'tab_item_'.rand(100,999);

    $activeClass = $active == 'y' ? 'active' :'';
    $output = '
        <li class="'.$activeClass.'">
            <a href="#'.$id.'">'.$title.'</a>
        </li>
    ';

    $tabs_divs.= '<div class="tab-pane '.$activeClass.'" id="'.$id.'">'.$content.'</div>';

    return $output;
}

以下是我标题中的内容:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

在我的页面上,我有:

[tabs]
[tab title="tab" active="y" id="home"]home[/tab]
[tab title="tab2" active="n" id="about"]about[/tab]
[tab title="tab3" active="n" id="help"]help[/tab]
[/tabs]

EDIT !!

function tabs_header() {

//wp_enqueue_script('jquery');

wp_register_script( 'add-jquery-js', 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', array('jquery'),'',true  );
wp_register_script( 'add-jqueryui-js', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', array('jquery'),'',true  );


wp_enqueue_style( 'add-jquery-js' );
wp_enqueue_style( 'add-jqueryui-js' );

}
add_action( 'wp_enqueue_scripts', 'tabs_header' );

1 个答案:

答案 0 :(得分:0)

除非你真的知道自己在做什么,don't enqueue external jQuery sources。大多数WordPress / jQuery错误都是因为这个原因。让WP使用其捆绑的jQuery版本,你就可以了。

您没有显示标签脚本,但我想您有一个。这就是我将 jQuery Tabs 脚本(I have this working plugin at GitHub for an Audio Player)排入队列的方式:

add_action( 'wp_enqueue_scripts', 'enqueue_scripts_so_17856211' );

function enqueue_scripts_so_17856211()
{
    wp_enqueue_style( 
        'tabs-css', 
        get_stylesheet_directory_uri() . '/css/tabs.css' 
    );
    wp_enqueue_script( 
        'tabs-js',
        get_stylesheet_directory_uri() . '/js/tabs.js', 
        array( 'jquery', 'jquery-ui' ), 
        false, 
        false 
    );
}

请注意,your-script.jsbeing enqueued,且jqueryjquery-ui依赖关系。

相关问题