将文件附件名称和URL添加到Javascript中

时间:2012-09-18 21:55:54

标签: javascript wordpress attachment

我正在努力将jPlayer合并到我们的WordPress网站中。我并不特别关心可用的两个插件,因为我们的客户端在技术上有些不足。我们要完成的是将标题和Mp3加载到jPlayer播放列表的JavaScript中。目前一切都正常加载并返回相应的文本,但是我无法正确加载文件名和附件URL。我对java和wordpress相当新。任何帮助,将不胜感激。如果您想查看实际网站示例,请访问http://www.ourgoodfight.com/?p=6054

<!--Begin Jplayer Playlist -->
<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function(){
        new jPlayerPlaylist({
            jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        }, [
         <?php
            global $post;
                $jukebox_args = get_posts( array(
                    'post_type' => 'attachment',
                    'posts_per_page' => -1,
                    'post_mime_type' => 'audio/mpeg',
                    'orderby' => 'menu_order',
                    'order'  => 'ASC',
                    'post_parent' => $post->ID
                        ));
                $the_jukebox_query = new WP_Query( $jukebox_args );        
        ?>
        <!--This is what needs to loop -->
            <?php
                $loop = new WP_Query( $jukebox_args );
                    while ( $loop->have_posts() ) : $loop->the_post(); ?>
                {
                    title:<?php echo apply_filters( 'the_title', $attachment->post_title ); ?>,
                    mp3:<?php echo wp_get_attachment_url( $attachment->ID, false ); ?>,
                },
            <?php endwhile;?>
        <!--This would be the end of the loop -->
        ], {
            swfPath: "<?php echo get_template_directory_uri(); ?>/js",
            supplied: "mp3",
            wmode: "window"
        });
    });
    //]]>
</script>

1 个答案:

答案 0 :(得分:0)

您正在尝试使用var $ attachment来访问附件数据。但是你没有设置$ attachment var实际包含任何数据,所以它当前是空的。因为您使用$ loop-&gt; the_post()在循环中设置了帖子数据;你应该能够;

title:<?php echo apply_filters( 'the_title', get_the_title() ); ?>,
mp3:<?php echo wp_get_attachment_url( get_the_ID(), false ); ?>,

http://codex.wordpress.org/Class_Reference/WP_Query

请注意使用wp_reset_postdata()重新设置发布数据的要点。