2017-04-15 29 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很多 –