2015-04-17 125 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源代码吗?你有多少个