2013-08-20 34 views
0

有没有办法从查询中拉出图片url,并将该url回显到同一页面的页脚中的插件脚本?我的问题是我有一个需要静态背景图像的图层滑块。问题在于查询运行在页面顶部,因此在页脚中调用javascript时,我无法访问该循环。获取wordpress特色图片URL到页脚脚本

我有这个疑问,正常的页面循环内:

/*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> 

回答

3

这将让图像

$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> 
+0

不幸的是,这是返回一个数组 – TripsLeft

+0

已更新,以回显数组中的图像源url – Simalam

+0

我已更新我的代码。我试图将新插入的WP_Query的图像URL插入脚本中的脚本中。您发布的代码正在获取实际页面本身的精选图片网址 – TripsLeft

0

这应该工作....

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

只是改变缩略图到你需要的图像尺寸。