2013-05-07 74 views
5

我目前正在嘗試爲創建標籤的WordPress創建短代碼,我知道有可用的短代碼..但我正在嘗試學習如何自己編寫它們。創建標籤的自定義短代碼

目前我正在生成標籤菜單項,但我堅持把內容放在一個全新的div中。

前端用法:

[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] 

然後代碼:

function tabs_group($atts, $content = null) { 
    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>'; 

    return $output; 
} 

add_shortcode("tabs", "tabs_group"); 

function tab($atts, $content = null) { 
    extract(shortcode_atts(array( 
    'id' => '', 
    'title' => '', 
    'active'=>'n' 
    ), $atts)); 
    if(empty($id)) 
     $id = 'tab_item_'.rand(100,999); 

    $output = '<li class="'.($active == 'y' ? 'active' :'').'"> 
       <a href="#'.$id.'">'.$title.'</a> 
       </li>'; 
    $output .= '<div class="tab-content"> 
       <div class="tab-pane active" id="'.$id.'">'.$content.'</div> 
       <div class="tab-pane" id="'.$id.'">'.$content.'</div> 
       <div class="tab-pane" id="'.$id.'">'.$content.'</div> 
       </div>'; 
    return $output; 
} 
add_shortcode("tab", "tab"); 

它目前返回:

<ul class="nav nav-tabs"> 
<li class="active"> 
       <a href="#home">tab</a> 
       </li><div class="tab-content"> 
       <div class="tab-pane active" id="home">home</div> 
       <div class="tab-pane" id="home">home</div> 
       <div class="tab-pane" id="home">home</div> 
       </div> 
<li class=""> 
       <a href="#about">tab2</a> 
       </li><div class="tab-content"> 
       <div class="tab-pane active" id="about">about</div> 
       <div class="tab-pane" id="about">about</div> 
       <div class="tab-pane" id="about">about</div> 
       </div> 
<li class=""> 
       <a href="#help">tab3</a> 
       </li><div class="tab-content"> 
       <div class="tab-pane active" id="help">help</div> 
       <div class="tab-pane" id="help">help</div> 
       <div class="tab-pane" id="help">help</div> 
       </div> 
</ul> 

,我需要它返回:

<ul class="nav nav-tabs " > 
<li class="active"> 
    <a href="#home">tab</a> 
</li> 
<li class=""> 
    <a href="#about">tab2</a> 
</li> 
<li class=""> 
    <a href="#help">tab3</a> 
</li> 
</ul> 

<div class="tab-content"> 
<div class="tab-pane active" id="home">blah</div> 
<div class="tab-pane" id="about">blah</div> 
<div class="tab-pane" id="help">blah</div> 
</div> 

我已經添加到輸出,謝謝Obmerk Kronen。

任何幫助非常感謝。

+4

你想了解 – 2013-05-07 08:14:23

+1

你沒有在你的內容 – 2013-05-07 08:21:50

+0

的div的一部分,我很抱歉,我不完全瞭解+1? – 2013-05-07 08:24:02

回答

2

這裏的問題是,你需要do_shortcode($content)後添加您的div:分析tab shorcodes,你必須保持你的div某處可以在之後輸出它們。

一個簡單方式(不是最好的方式)這樣做可以使用全局變量,例如:

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; 
} 
+0

嘿謝謝,你的回答爲我工作..「不是最好的方式」 ,就像我正在學習和編寫好的代碼一樣...所以這是一個壞習慣或者? – 2013-05-07 09:13:26

+0

你應該閱讀關於使用全局變量的這個:http://stackoverflow.com/questions/5166087/php-global- in-functions – soju 2013-05-07 09:22:34

2

你現在還沒有的div的內容部分內容

$output .= '<div class="tab-content"> 
<div class="tab-pane active" id="'.$id.'">'.$title.'</div> 
<div class="tab-pane" id="'.$id.'">'.$title.'</div> 
<div class="tab-pane" id="'.$id.'">'.$title.'</div> 
</div>' 
+0

謝謝你的帖子是非常有用的..但它並沒有解決問題..請參閱更新.. :) – 2013-05-07 08:35:11

+1

@Dawid van der Hoven - 顯然沒有解決問題,因爲你把它放在了錯誤的地方:-)。但是由於我評論和回答某人已經以不同的方式粘貼了相同的解決方案。你應該在另一個函數中粘貼我的代碼。 – 2013-05-07 09:44:43

+0

對不起,但你錯了,仔細看看我的答案,你會發現它是不一樣的'在不同的味道'... – soju 2013-05-08 11:20:36

0
<?php 
// my shortcode look like this. 
[tabgroup] 
    [tab title="Your title #1" class="active" ] 
    Your Description here #1 
    [/tab] 
    [tab title="Your title #2" ] 
    Your Description here #2 
    [/tab]  
    [tab title="Your title #3" ] 
    Your Description here #3 
    [/tab]  
[/tabgroup] 

add_shortcode('tab', 'ease_tabs'); 
function ease_tabs($atts, $content){ 
    extract(shortcode_atts(array(
     'title' => 'Tab', 
     'class' => false, 
    ), $atts)); 

    $x = isset($GLOBALS['tab_count'])?$GLOBALS['tab_count']:0; 
    $GLOBALS['tab_count'] = isset($GLOBALS['tab_count'])?$GLOBALS['tab_count']:0;   

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

    $GLOBALS['tab_count']++; 

} 

add_shortcode('tabgroup', 'ease_tabgroup'); 
function ease_tabgroup($atts, $content){ 

    if(isset($GLOBALS['tabs_count'])) 
     $GLOBALS['tabs_count']++; 
    else 
     $GLOBALS['tabs_count'] = 0; 

    do_shortcode($content); 

    if(is_array($GLOBALS['tabs'][$GLOBALS['tabs_count']])){ 

     $tabs=array(); 
     $panes=array(); 

     foreach($GLOBALS['tabs'][$GLOBALS['tabs_count']] as $tab){ 

     $panes_class = (!empty($tab["class"]) && $tab['class'] == "active") ? 'tab-pane active' : 'tab-pane'; 
     $__title = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($tab['title']) );  
     $tabs[] = '<li role="presentation" class="'.$tab['class'].'" > 
         <a href="#'.$__title.'" data-toggle="tab">'.$tab['title'].'</a> 
        </li>'; 
     $panes[] = sprintf('<div class="%s" id="%s"> %s </div>',$panes_class, $__title, $tab['content'] ); 

     } 


     $return = "\n".'<div role="tabpanel"> 
          <ul role="tablist" class="nav nav-tabs">' 
           .implode("\n", $tabs). 
          '</ul>'."\n". 
          '<div class="tab-content">' 
            .implode("\n", $panes). 
          '</div> 
         </div>'."\n";  
    } 
    return do_shortcode(sprintf('<div class="tp-tabs"> %s </div>', $return)); 
} 

+0

請在回答中提供一些關於問題原因以及代碼如何解決問題的詳細信息。 – 2015-06-29 13:09:50

相關問題