2013-02-01 46 views
0

插件代码短代码没有给我想要输出的WordPress

function myShortcode($atts) { 
    extract(shortcode_atts(array('cat' => '',), $atts)); 
    ob_start(); 
    if ($cat == '') { echo "Do Nothing!"; } 
    $output_string = ob_get_contents(); 
    ob_end_clean(); 
    return $output_string; 
} 
add_shortcode(mycatlist, myShortcode); 

添加以下简称代码后(在后段从可湿性粉剂管理员)

[mycatlist cat=4] 

添加以下代码在wordpress模板文件中

<?php echo do_shortcode("[mycatlist]"); ?> 

我得到那个输出什么都不做!

,但我想猫值我的意思是4

+0

@Mark。是的,我尝试过,但没有工作 –

+1

我不明白你为什么需要'ob_'在这段代码中。 ::::: *“给我输出”*和*“需要输出”*代表什么? ::::据我所见,第一个例子('[mycatlist cat = 4]')有一个空输出,第二个('do_shortcode(“[mycatlist]”)')应该输出'Do Nothing!' ... – brasofilo

+0

@brasofilo请再次检查我编辑的问题 –

回答

0

您发布的代码有很多的代码,已经没有太大的做你的测试方案。它简化为这样:

function myShortcode($atts) { 

    // maybe add some debugging? 
    // print_r($atts); 

    extract(shortcode_atts(array('cat' => '',), $atts)); 
    return $cat; 
} 
add_shortcode('mycatlist', 'myShortcode'); 

现在这样称呼它:

<?php echo do_shortcode("[mycatlist cat=\"4\"]"); ?> 

应该在页面上打印4。

+0

谢谢@Mark,我正在尝试该代码 –

+0

不是代码<?php echo do_shortcode(“[mycatlist cat = \”4 \“]”); ?>不能为我工作,bcoz我在模板页面中使用该代码,我使用该模板页面很多页面时,我从管理员创建页面 –