将wordpress精选图片网址添加到页脚脚本中

时间:2013-08-20 13:25:05

标签: wordpress loops footer

有没有办法从查询中提取图片网址,并将该网址回显到同一网页页脚中的插件脚本?我的问题是我有一个需要静态背景图像的layerlider。问题是查询是在页面顶部运行的,所以在页脚调用javascript时我无法访问循环。

我有这个查询,正常页面循环的INSIDE:

/*Normal Page Loop Here*/ 
if (have_posts()) : while (have_posts()) : the_post();

/*Begin Secondary Query to be Inserted into this page*/
$args = array(
'post_type' => 'projects',
'orderby' => 'rand',
'posts_per_page' => 1,
'meta_query' => array(
           array(
            'key' => 'custom_featured',
            'value' => 'on',
            )
        )
);
$my_query = new WP_Query($args);


    /*Output Results of Internal Page Query*/
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
       the_title();
       the_post_thumbnail('i-need-this-url-in-the-footer-script');        

    endwhile;/*End the secondary query loop*/
    wp_reset_query();


endwhile; endif;/*End the page loop*/    

我基本上需要将这个新WP_Query的特色图像的URL插入到页脚中的脚本中:

<script type="text/javascript">
    //Layer Slider
       $(document).ready(function(){
       $('#layerslider').layerSlider({
       globalBGImage: '<?php echo $imagefromloopurlhere; ?>'
     });
  });
</script>

2 个答案:

答案 0 :(得分:3)

这将获得图像的URL

$image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );  
if($image_src)
     echo $image_src[0];

为了将变量放入页脚,您可以执行以下操作:

global $project_featured_url;
while ($my_query->have_posts()) : $my_query->the_post();
   the_title();
   $image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );  
   if($image_src)
      $project_featured_url = $image_src[0];
   else
       $project_featured_url = 'some default url to avoid javascript error';
endwhile;/*End the secondary query loop*/

然后在你的页脚

<?php global $project_featured_url; ?>
<script type="text/javascript">
    //Layer Slider
       $(document).ready(function(){
       $('#layerslider').layerSlider({
       globalBGImage: '<?php echo $project_featured_url; ?>'
     });
  });
</script>

答案 1 :(得分:0)

这应该有效....

<?php get_the_post_thumbnail($post->ID, 'thumbnail'); ?>

只需将缩略图更改为您需要的图像尺寸。