0

插件代码

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);

在帖子中添加以下短代码(在 wp-admin 的帖子部分中)

    [mycatlist cat=4]

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

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

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

但我想得到 cat 值,我的意思是 4

4

1 回答 1

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。

于 2013-02-01T13:01:34.423 回答