dqb14659 2013-09-26 14:52
浏览 42

jQuery Backstretch和PHP

I am building a WordPress website where I want a fullscreen homepage background slider. I really like the jQuery backstretch plugin here: https://github.com/srobbin/jquery-backstretch

(If you take a quick peek at how it works there, choose the second on the Demo dropdown.)

To make the plugin work you need to use this JS snippet:

            <script>            
        $.backstretch([ 
              "http://dl.dropbox.com/u/515046/www/outside.jpg"
            , "http://dl.dropbox.com/u/515046/www/garfield-interior.jpg"
            , "http://dl.dropbox.com/u/515046/www/cheers.jpg"
          ], {duration: 3000, fade: 750});
        </script>

As you can see, the images need to be declared within the JS. I need to be able to upload as many images as I want, run a loop to get the URL's for each image then display them. I will need to pass these via PHP.

The PHP snippet for getting each image will be: <?php the_field('background_image'); ?>

How can I alter the script to run a loop and get the image/s via PHP?

Thanks in advance

Edit:

This is the loop to display all of my images using ACF plugin and the options page add-on: '

    <ul>

    <?php while(has_sub_field('slider_images','option')): ?>

        <li><img src="<?php the_sub_field('slider_image'); ?>" /></li>

    <?php endwhile; ?>

    </ul>

<?php endif; ?>' 
  • 写回答

2条回答 默认 最新

  • dougu5847 2013-09-27 09:19
    关注

    It depends on how these images are associated with posts/options, that is not clear from your question. The PHP you've suggested there looks like an option attached to an individual page rather than array full of images. However for the sake of your example let's assume that you want to pull the background image out of the first 5 posts in a custom post type called home_slider and use those;

            <?php
    
                $imgList = '';
    
                $homeslider_query = new WP_Query(array('post_type' => 'home_slider', 'numberposts' => '5', 'orderby' => 'menu_order', 'order' => 'ASC' ));
    
                if ( $homeslider_query->have_posts() ) :  
                    while ($homeslider_query->have_posts()) : $homeslider_query->the_post(); 
    
                        // Build your list of images
                        $imgList .= '"'. the_field('background_image') .'",';
    
                    endwhile;
                endif;
    
                // Trim trailing comma from list
                $imgList = rtrim($imgList, ',');
    
            ?>
    

    Now, as you have the script tags there that tells me the JS is in the page itself, probably the head, rather than a .js file so;

            <script>            
            $.backstretch([ 
                  <?php echo $imgList; ?>
              ], {duration: 3000, fade: 750});
            </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题