【问题标题】:Wordpress ShortcodeWordPress 简码
【发布时间】:2012-01-11 06:42:41
【问题描述】:

我想创建一个类似这样的多个短代码。

[tabs]

[tab title="1"]
//content goes here
[/tab]

[tab title="2"]
//content2 goes here
[/tab]

[tab title="3"]
//content4 goes here
[/tab]

[/tabs]

预期输出:

<ul>
    <li id="tab1">[tab title="1" goes here]</li>
    <li id="tab2">[tab title="2" goes here]</li>
    <li id="tab3">[tab title="3" goes here]</li>
</ul>

<ul id="tab1">
    <li>Content1</li>
</ul>

<ul id="tab2">
    <li>Content2</li>
</ul>

<ul id="tab3">
    <li>Content3</li>
</ul>

如何在 WordPress 中做到这一点?

【问题讨论】:

标签: wordpress shortcode


【解决方案1】:

首先创建“标签”短代码

function tabs_shortcode( $atts, $content ) {
    $atts = shortcode_atts( array(      
            'id' => ''        
            ), $atts );

extract ($atts); // gives you the ability to use array keys as variables

    return '<ul id="'. $id.'">'. do_shortcode( $content ) .'</ul>';

}
add_shortcode( 'tabs', 'tabs_shortcode' );

现在创建“tab”短代码

function tab_shortcode( $atts, $content ) {
   $atts = shortcode_atts( array(       
           'id' => ''        
           ), $atts );

extract ($atts); // gives you the ability to use array keys as variables

    return '<li id="'. $id.'">'. do_shortcode( $content ) .'</li>';

}
add_shortcode( 'tab', 'tab_shortcode' );

这样使用

[tabs id="tab"]
   [tab id="tab1"] lorem ipsum dolor [/tab]
   [tab id="tab2"] lorem ipsum dolor [/tab]
   [tab id="tab3"] lorem ipsum dolor [/tab]
[/tab]

【讨论】:

    【解决方案2】:
    add_shortcode('external', 'externalFunction');
    function externalFunction( $atts,$content = null){
         echo do_shortcode('[internal]'.$content.'[/internal]');
    }
    
    add_shortcode('internal','internalFunction');
    function internalFunction($atts, $content=null)
    {
      extract(shortcode_atts(
          array(
                "title" => ''
            ), $atts));  
          /* do your stuffs here */
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多