将小部件中的自定义快捷代码强制放在小部件的顶部

时间:2011-01-11 作者:agileapricot

我制作的自定义短代码被强制到小部件容器外部的小部件顶部。知道为什么吗?这是我的代码。。。

function nktmediaplayer_func($atts) {
 extract(shortcode_atts(array(
    \'id\' => rand(1, 900),
    \'language\' => \'en\',
    \'playlist\' => \'no\',
    \'media\' => \'3381\',
    \'height\' => \'480\',
    \'width\' => \'640\',
    \'style\' => \'single\'
    ), $atts));
    ?>

    <div id="player_<?php echo $id; ?>" class="video_player"><a href="http://www.adobe.com/products/flashplayer/">Get the Flash Player</a> to see this player.</div>

    <script type="text/javascript" src="http://kadampa.org/embed/apps/jwplayer.js"></script> 
    <script type="text/javascript"> 
        jwplayer("player_<?php echo $id; ?>").setup({
            flashplayer: "http://kadampa.org/embed/apps/player.swf",
            playlistfile: "http://kadampa.org/<?php echo $language; ?>/api/video/<?php if ( \'playlist\' == \'yes\' ) echo \'playlist/\'; ?><?php echo $media; ?>/desc",
            height: "<?php echo $height; ?>",
            width: "<?php echo $width; ?>",
            config: "http://kadampa.org/embed/config/<?php echo $style; ?>.xml"
        });
    </script>

    <?php    
}
add_shortcode(\'nkt_mediaplayer\', \'nktmediaplayer_func\', 10);

1 个回复
最合适的回答,由SO网友:sanchothefat 整理而成

对于短代码,您必须返回输出,以便在短代码出现的地方写出。

要么将HTML转换为PHP字符串,而不是打破PHP标记,要么可以使用PHPs输出缓冲方法,如下所示:

ob_start();

?>

    <div id="player_<?php echo $id; ?>" class="video_player"><a href="http://www.adobe.com/products/flashplayer/">Get the Flash Player</a> to see this player.</div>

    <script type="text/javascript" src="http://kadampa.org/embed/apps/jwplayer.js"></script> 
    <script type="text/javascript"> 
        jwplayer("player_<?php echo $id; ?>").setup({
            flashplayer: "http://kadampa.org/embed/apps/player.swf",
            playlistfile: "http://kadampa.org/<?php echo $language; ?>/api/video/<?php if ( \'playlist\' == \'yes\' ) echo \'playlist/\'; ?><?php echo $media; ?>/desc",
            height: "<?php echo $height; ?>",
            width: "<?php echo $width; ?>",
            config: "http://kadampa.org/embed/config/<?php echo $style; ?>.xml"
        });
    </script>

<?php

$output = ob_get_contents();
ob_end_clean();

return $output; 

结束

相关推荐

Why use widgets?

我对使用WordPress很陌生,我想知道使用小部件的好处是什么?看here 这听起来像是为那些不是程序员的人准备的,他们想在他们的网站上添加插件。对吗?或者小部件是否在某种程度上使站点更加健壮?