2017-04-15 59 views
-1

我有下面的代碼,我想它使用我如何在wordpress中創建短代碼?

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

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

該做什麼

內容DOS這裏

上顯示的內容執行注意事項

不要忘記的內容

代碼想使用

// 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

你要面臨的第一個問題是使用一個單一的函數內部多個返回語句。第一次返回後的任何內容都不會被執行。

第二個問題是你傳遞內容的方式。您的屬性數組中有一個名爲content的元素。如果您在該數組上運行提取,它將覆蓋短代碼回調的參數$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很多 –