WordPress Shortcode自定义属性

时间:2018-01-31 12:03:38

标签: wordpress plugins attributes shortcode

我有这个短代码[my_shortcode],如何添加属性并根据值进行检索? 例如:[my_shortcode =" website"]或[my_shortcode =" landingpage"]

function my_shortcode_add(){
    wp_enqueue_script('my-script-1');
    wp_enqueue_style('my-css'); 
    ob_start();
    include("include/shortcode.php");
    return ob_get_clean();
  }
add_shortcode('my_shortcode', 'my_shortcode_add');

谢谢!

1 个答案:

答案 0 :(得分:1)

替换为:

function my_shortcode_add($atts){
    wp_enqueue_script('my-script-1');
    wp_enqueue_style('my-css'); 
    ob_start();
    include("include/shortcode.php");
    if($atts['website'] == '1'){
       echo 'website work';
    }
    if($atts['landingpage'] == '1'){
       echo 'landingpage work';
    }
    return ob_get_clean();
  }
add_shortcode('my_shortcode', 'my_shortcode_add');

有了这个,您可以使用短代码中的属性

[my_shortcode website=1]
[my_shortcode website=0 landingpage=1]
[my_shortcode landingpage=1]