2015-04-17 124 views
3

所以我有這個修改後的Wordpress循環。 Wordpress Modified/Super Loop的任務是爲每篇文章分配一個唯一的HTML結構。改進的Wordpress循環中的精選圖像

無論如何,我成功地使用它。直到我添加了調用特色圖像並將其設置爲背景圖像(css樣式屬性)的代碼。

我現在遇到的錯誤是它爲所有帖子提供相同的精選圖片。例如,對於帖子號碼2至6,,它顯示屬於帖子2的相同特色圖像

<?php if (have_posts()) : ?> 
<?php $count = 0; ?> 
<?php while (have_posts()) : the_post(); ?> 
<?php $count++; ?> 
<?php if ($count >= 2 && $count <= 6) : ?> 



//code to pull the featured images' urls 

    <?php 
    global $post; 

    if (has_post_thumbnail()) { //if a thumbnail has been set 
    $imgID = get_post_thumbnail_id($post->ID); //get the id of the featured image 
    $featuredImage1 = wp_get_attachment_image_src($imgID, 'TypeTwo-1');//get the url of the featured image (returns an array) 
    $featuredImage2 = wp_get_attachment_image_src($imgID, 'TypeThree-2');//get the url of the featured image (returns an array) 

    $posttypetwoURL1 = $featuredImage1[0]; //get the url of the image out of the array 
    $posttypetwoURL2 = $featuredImage2[0]; //get the url of the image out of the array 
    ?> 


    <style type="text/css"> 

    .pt2s-img { 
    border:none; 
    color:black; 
    background-image: url(<?php echo $posttypetwoURL1 ?>); 
    background-repeat:no-repeat; 
    width:484px; 
    height:350px; 
    display:inline-block; 
    } 

    @media screen and (max-width:1231px) { 
     .pt2s-img { 
     border:none; 
     color:black; 
     background-image: url(<?php echo $posttypetwoURL2 ?>); 
     background-repeat:no-repeat; 
     width:484px; 
     height:350px; 
     } 
    } 

    @media screen and (max-width:800px) { 
     .pt2s-img { 
     border:none; 
     color:black; 
     background-image: url(<?php echo $imgURL3 ?>); 
     background-repeat:no-repeat; 
     width:150px; 
     height:250px; 
     } 
    } 

    </style> 

    <?php 
    }//end if 

    ?> 

    <a class="pt2s-img" href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" > 
    </a> 

//code to pull the featured images' urls  


<?php endif; ?> 
<?php endwhile; ?> 
<?php endif; ?> 

我懷疑這個代碼是搞亂循環:

$imgID = get_post_thumbnail_id($post->ID); //get the id of the featured image 
    $featuredImage1 = wp_get_attachment_image_src($imgID, 'TypeTwo-1');//get the url of the featured image (returns an array) 
    $featuredImage2 = wp_get_attachment_image_src($imgID, 'TypeThree-2');//get the url of the featured image (returns an array) 

    $posttypetwoURL1 = $featuredImage1[0]; //get the url of the image out of the array 
    $posttypetwoURL2 = $featuredImage2[0]; //get the url of the image out of the array 

,但我不確定。我認爲這些代碼應該放在某個地方,這樣不會混淆循環並顯示正確的圖像。

有關正確執行此操作的任何建議?

回答

1

has_post_thumbnail()接受郵編ID作爲其參數,改爲使用has_post_thumbnail($post->ID)。因爲您正在使用修改後的循環,所以您需要隱式傳遞帖子ID作爲函數參數。

+0

試過這個,但沒有發生。 –

+0

實際上,它的工作原理是使用標記,但問題是圖像應該作爲背景圖像。這樣,我可以調用我想要的尺寸。 –

+0

您可以在網頁瀏覽器中打開網頁並查看HTML源代碼嗎?你有多少個