0

クエリから画像の URL を取得し、その URL を同じページのフッターにあるプラグイン スクリプトにエコーする方法はありますか? 私の問題は、静的な背景画像を必要とするレイヤースライダーがあることです。問題は、クエリがページの上部で実行されるため、フッターで JavaScript が呼び出されるまでにループにアクセスできなくなることです。

通常のページループの内側に、次のクエリがあります。

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

2 に答える 2

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>
于 2013-08-20T16:37:16.530 に答える