Frage

I Am trying to create a wordpress shortcode that allows users to creat jQuery UI tabs. I found this great script online which claims to do it - however it has some quite unwelcome results in my page.

Firstly, the script I am currently using is as follows:

add_shortcode( 'tabgroup', 'jqtools_tab_group' );
function jqtools_tab_group( $atts, $content )
{
    $GLOBALS['tab_count'] = 0;

    do_shortcode( $content );

    if( is_array( $GLOBALS['tabs'] ) ){
        foreach( $GLOBALS['tabs'] as $tab ){
            $tabs[] = '<li><a class="" href="#">'.$tab['title'].'</a></li>';
            $panes[] = '<div class="pane"><h3>'.$tab['title'].'</h3>'.$tab['content'].'</div>';
        }
        $return = "\n".'<!-- the tabs --><ul class="tabs">'.implode( "\n", $tabs ).'</ul>'."\n".'<!-- tab "panes" --><div class="panes">'.implode( "\n", $panes ).'</div>'."\n";
    }
    return $return;
}

add_shortcode( 'tab', 'jqtools_tab' );
function jqtools_tab( $atts, $content )
{
    extract(shortcode_atts(array(
            'title' => 'Tab %d'
    ), $atts));

    $x = $GLOBALS['tab_count'];
    $GLOBALS['tabs'][$x] = array( 'title' => sprintf( $title, $GLOBALS['tab_count'] ), 'content' =>  $content );

    $GLOBALS['tab_count']++;
}

However, the HTML output I am TRYING to achieve is this:

<div class="tabs">
    <ul>
        <li><a href="#tabs-1">Tab 1</a></li>
        <li><a href="#tabs-2">Tab 2</a></li>
        <li><a href="#tabs-3">Tab 3</a></li>
    </ul>
    <div id="tabs-1">
        <p>Content</p>
    </div>
    <div id="tabs-2">
        <p>Content</p>
    </div>
    <div id="tabs-3">
        <p>Content</p>
    </div>
</div>

The shortcode php gives a slightly different output to what I need in order to make this work - here is my current outputted HTML:

<!-- the tabs -->
<ul class="tabs">
<li><a class="" href="#">Tab 0</a></li>
<li><a class="" href="#">Tab 1</a></li>
<li><a class="" href="#">Tab 2</a></li>
</ul>
<!-- tab "panes" -->
<div class="panes">
    <div class="pane">
        <h3>Tab 0</h3>Content
    </div>
    <div class="pane">
        <h3>Tab 1</h3>Content
    </div>
    <div class="pane">
        <h3>Tab 2</h3>Content
    </div>
</div>

Finally, the shortcode I am using looks like this:

[tabgroup]

[tab title="tab1"]Content[/tab]

[tab title="tab2"]Content[/tab]

[tab title="tab3"]Content[/tab]

[/tabgroup]

My question is, how do I need to change my php code to make the outputted HTML look like the HTML needed to make the tabs work?

War es hilfreich?

Lösung

OK this is what you have to do.

if( is_array( $GLOBALS['tabs'] ) ){
    foreach( $GLOBALS['tabs'] as $k=>$tab ){
        $tabs[] = '<li><a href="#tab-'.$k.'">'.$tab['title'].'</a></li>';
        $panes[] = '<div id="tab-'.$k.'"><p>'.$tab['content'].'</p></div>';
    }
    $return = "\n".'<!-- the tabs --><div class="tabs"><ul>'.implode( "\n", $tabs ).'</ul>'."\n".'<!-- tab "panes" -->'.implode( "\n", $panes ).'</div>'."\n";
}

Andere Tipps

You can also add shortcode like this:-

// Tab
function tab_shortcode( $atts, $content = null ) {
    extract( shortcode_atts( array(
        'title' => ''
    ), $atts ) );
    return '<div id="tabs-'. sanitize_title( $title ) .'">'. do_shortcode( $content ) .'</div>';
}
add_shortcode( 'tab', 'tab_shortcode' );

// Tabs Container
function tabs_container_shortcode( $atts, $content = null ) {

    preg_match_all( '/tab title="([^\"]+)"/i', $content, $matche, PREG_OFFSET_CAPTURE );

    $tab_title = array();

    if( isset($matche[1]) ) {
        $tab_title = $matche[1];
    }

    $output = '';

    if( count($tab_title) ) {
        $output .= '<div id="tabs">';
        $output .= '<ul class="nav clearfix">';
        foreach( $tab_title as $tab ){
            $output .= '<li><a href="#tabs-'. sanitize_title( $tab[0] ) .'">' . $tab[0] . '</a></li>';
        }
        $output .= '</ul>' . do_shortcode( $content ) . '</div>';
    } else {
        $output .= do_shortcode( $content );
    }

    return $output;

}
add_shortcode( 'tabs_container', 'tabs_container_shortcode' );

This will also work for tabs. Hope this will help you bit.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top