2017-04-15 5 views
-1

私は以下のコードを持っていると私はそれがどのように私はwordpressでショートコードを作成できますか?

いけないことここに

DOS用

コンテンツ

[insert_dos]*Content for dos here*[/insert_dos] 

[insert_donts]*Content for dos here*[/insert_donts] 

ドスを使用して実装上のコンテンツを表示したいですここに記載されていないコンテンツ

コードが

// Shortcode for dos 
     function insert_dos_func($atts,$content) { 
    extract(shortcode_atts(array(
     'content' => 'Hello World', 
     ), $atts)); 

     return '<h2>DOs</h2>'; 
     return '<div>' . $content . '</div>'; 
    } 
    add_shortcode('insert_dos', 'insert_dos_func'); 



// Shortcode for don'ts 
     function insert_donts_func($atts) { 
      extract(shortcode_atts(array(
      'content' => 'Hello World', 
      ), $atts)); 

      return "<h2>DON'Ts</h2>"; 
      return "<div>" . $content . "</div>"; 
     } 
     add_shortcode('insert_donts', 'insert_donts_func'); 
+0

2返品は機能しません。一度機能を終了すると、次は実行されません。 – charlietfl

答えて

1

を使用しようとしていますあなたが直面しようとしている最初の問題は、単一の関数内に複数のreturn文を使用することです。最初の復帰後は何も実行されません。

2番目の問題は、コンテンツを渡す方法です。属性配列には、contentという名前の要素があります。その配列でextractを実行すると、ショートコードコールバックの引数$contentが上書きされます。

function insert_dos_func($atts, $content) { 

    /** 
    * This is going to get attributes and set defaults. 
    * 
    * Example of a shortcode attribute: 
    * [insert_dos my_attribute="testing"] 
    * 
    * In the code below, if my_attribute isn't set on the shortcode 
    * it's going to default to Hello World. Extract will make it 
    * available as $my_attribute instead of $atts['my_attribute']. 
    * 
    * It's here purely as an example based on the code you originally 
    * posted. $my_attribute isn't actually used in the output. 
    */ 
    extract(shortcode_atts(array(
     'my_attribute' => 'Hello World', 
    ), $atts)); 

    // All content is going to be appended to a string. 
    $output = ''; 

    $output .= '<h2>DOs</h2>'; 
    $output .= '<div>' . $content . '</div>'; 

    // Once we've built our output string, we're going to return it. 
    return $output; 
} 
add_shortcode('insert_dos', 'insert_dos_func'); 
+0

私に必要なもの。 Thxsたくさん –

関連する問題