首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Supersized.js如何将JSON编码的数据调用到设置脚本中

Supersized.js如何将JSON编码的数据调用到设置脚本中
EN

Stack Overflow用户
提问于 2011-05-21 07:42:10
回答 3查看 1.8K关注 0票数 0

好吧,在这方面我还是个新手,但这里是这样:

我正在使用wordpress网站上的supersized.js为首页创建全尺寸的背景图像幻灯片,这足以说明脚本已经设置好了,现在它可以工作了。我的下一个问题是让脚本使用wp_attachment来拉取图像

在我的functions.php文件中,我创建了以下内容:

代码语言:javascript
复制
// Get all of the images attached to the current post
// These images will be used in the Supersized homepage gallery
function supersized_get_images($size = 'full') {
    global $post;

    $photos = get_children( array('post_parent' => $post->ID, 'post_status' =>     'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

    $results = array();

    if ($photos) {
        foreach ($photos as $photo) {
        // get the correct URL for the selected size
        $results['image'] = wp_get_attachment_url($photo->ID);
    }
}
// encode into JSON format and pass to javascript supersettings.js
echo json_encode($results); 
}

所以不管怎样(我把echo放进去),因为我想看看它是不是生成了正确的JSON格式。echo上的输出如下所示:

代码语言:javascript
复制
{"image":"http:\/\/pilarcorrias.secondvariety.org\/wp-content\/uploads\/0bcf5aa159739b260a77758c7d33699b.jpg"}

我假设这是正确的。Supersized有一个如下所示的设置文件:

代码语言:javascript
复制
jQuery(function($){
            $.supersized({

                //Functionality
                slideshow               :   1,      //Slideshow on/off
                autoplay                :   1,      //Slideshow starts playing automatically
                start_slide             :   1,      //Start slide (0 is random)
                random                  :   0,      //Randomize slide order (Ignores start slide)
                slide_interval          :   3000,   //Length between transitions
                transition              :   1,      //0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
                transition_speed        :   500,    //Speed of transition
                new_window              :   1,      //Image links open in new window/tab
                pause_hover             :   0,      //Pause slideshow on hover
                keyboard_nav            :   1,      //Keyboard navigation on/off
                performance             :   1,      //0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
                image_protect           :   1,      //Disables image dragging and right click with Javascript
                image_path              :   '/../../../slideshow/', //Default image path

                //Size & Position
                min_width               :   0,      //Min width allowed (in pixels)
                min_height              :   0,      //Min height allowed (in pixels)
                vertical_center         :   1,      //Vertically center background
                horizontal_center       :   1,      //Horizontally center background
                fit_portrait            :   1,      //Portrait images will not exceed browser height
                fit_landscape           :   0,      //Landscape images will not exceed browser width

                //Components
                navigation              :   1,      //Slideshow controls on/off
                thumbnail_navigation    :   1,      //Thumbnail navigation
                slide_counter           :   1,      //Display slide numbers
                slide_captions          :   1,      //Slide caption (Pull from "title" in slides array)
                slides                  :   [       //Slideshow Images
                                                    {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.1/slides/quietchaos-kitty.jpg', title : 'Quiet Chaos by Kitty Gallannaugh', url : 'http://www.nonsensesociety.com/2010/12/kitty-gallannaugh/'},  
                                                    {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.1/slides/wanderers-kitty.jpg', title : 'Wanderers by Kitty Gallannaugh', url : 'http://www.nonsensesociety.com/2010/12/kitty-gallannaugh/'},  
                                                    {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.1/slides/apple-kitty.jpg', title : 'Applewood by Kitty Gallannaugh', url : 'http://www.nonsensesociety.com/2010/12/kitty-gallannaugh/'}  
                                            ]


            }); 
        });

该文件的最后一行声明了参数slides,然后将图像引用传递给幻灯片。现在我已经检查过了,幻灯片在没有网址和标题信息的情况下运行得很好,这意味着我只需要给它一个image对象,这里是文件URI,这意味着我的一些JSON应该可以逐字工作。现在我知道我正确地编码了PHP数组,我如何将它放入上面的supersettings.js文件中…我到处寻找,但没有找到任何可以解释它的东西,我可以用我的小大脑来解释它。任何帮助都将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2012-01-30 08:59:55

干得好!此外,如果要从数组中排除缩略图图像:

代码语言:javascript
复制
function supersized_get_images($size = 'full') {
global $post;
$thumb_id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID
$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'exclude' => $thumb_id) );
票数 1
EN

Stack Overflow用户

发布于 2011-05-23 20:34:57

解决了它,我是一个数字-我只是把脚本放在函数文件中,并将其命名为wp-footer钩子,它允许我在脚本本身中回显json变量。这里是我完整使用的代码,供其他试图使用supersized和Wordpress附件系统的人参考:

调用附件并创建json数组:

代码语言:javascript
复制
// Get all of the images attached to the current post
// These images will be used in the Supersized homepage gallery

function supersized_get_images($size = 'full') {
global $post;

$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

$results = array();

if ($photos) {
    foreach ($photos as $photo) {
    $keys [] = $photo->ID;
    $captions [] = $photo->post_excerpt;
    $descriptions [] = $photo->post_content;
        // get the correct URL for the selected size
        $results[] = array('image' => wp_get_attachment_url($photo->ID, 'full'), 'title' => '', 'url' => get_attachment_link($photo->ID));
    }
}
return str_replace('\/', '/', json_encode($results));
}

好了,这样就得到了图像,创建了数组,也正确地格式化了URL,去掉了转义的斜杠,看起来像这样: http:\/\/www。接下来,我必须将脚本嵌入到主体标记之前的页脚中,如下所示:

代码语言:javascript
复制
function super_settings() { ?>
<script type="text/javascript">
jQuery(function($){
            $.supersized({

                //Functionality
                slideshow               :   1,      //Slideshow on/off
                autoplay                :   1,      //Slideshow starts playing automatically
                start_slide             :   1,      //Start slide (0 is random)
                random                  :   0,      //Randomize slide order (Ignores start slide)
                slide_interval          :   3000,   //Length between transitions
                transition              :   1,      //0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
                transition_speed        :   500,    //Speed of transition
                new_window              :   1,      //Image links open in new window/tab
                pause_hover             :   0,      //Pause slideshow on hover
                keyboard_nav            :   1,      //Keyboard navigation on/off
                performance             :   1,      //0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
                image_protect           :   1,      //Disables image dragging and right click with Javascript
                image_path              :   '/../../../slideshow/', //Default image path

                //Size & Position
                min_width               :   0,      //Min width allowed (in pixels)
                min_height              :   0,      //Min height allowed (in pixels)
                vertical_center         :   1,      //Vertically center background
                horizontal_center       :   1,      //Horizontally center background
                fit_portrait            :   1,      //Portrait images will not exceed browser height
                fit_landscape           :   0,      //Landscape images will not exceed browser width

                //Components
                navigation              :   0,      //Slideshow controls on/off
                thumbnail_navigation    :   1,      //Thumbnail navigation
                slide_counter           :   1,      //Display slide numbers
                slide_captions          :   1,      //Slide caption (Pull from "title" in slides array)
                slides                  :   <?php echo supersized_get_images(); ?>


            }); 
        });

</script>
<?php }
add_action('wp_footer', 'super_settings');

这将向wp_footer添加一个操作,该操作调用super_settings函数,将脚本嵌入到脚注中,您可以看到脚本中的最后一行回显supersized_get_images()函数,并将键和值数组直接输出到javascript中。

我很高兴我可以自己回答这个问题,因为我觉得问这个问题很尴尬,但我希望这能帮助那些想要使用supersized的人,而不必使用wordpress中的自定义上传路径或FTP -只需使用标准的wordpress帖子库即可。]您所要做的就是在您想要显示背景的页面上包含supersized.js和设置脚本。维奥拉!如果任何人有改进,他们可以通过各种方式提出建议,请在这里张贴。

票数 0
EN

Stack Overflow用户

发布于 2015-06-10 02:53:23

否则,如果你不想混淆php、json、php、json等等;如果你想要一种清晰的方法来解析json文件,那就用getJson来实现。

代码语言:javascript
复制
            jQuery(function($){


            var urltojson = 'getjson.json';




            $.getJSON(urltojson, function(photos){


            $.supersized({



                // Functionality

                slide_interval          :   5000,       // Length between transitions

                transition              :   6,          // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left

                transition_speed        :   1000,       // Speed of transition



                // Components                           

                slide_links             :   'blank',    // Individual links for each slide (Options: false, 'num', 'name', 'blank')

                slides                  :   photos



            });
            });

        });

如果Json完全加载,则将启动supersized。这一切都没有用Javascript编写肮脏的php代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6078602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档