所以我有这个修改过的Wordpress循环。 Wordpress Modified/Super Loop的任务是为每个帖子分配一个唯一的HTML结构。
无论如何,我成功地使用了它。直到我添加调用特色图像的代码并使其成为背景图像(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
但我不确定。我认为那些代码应放在某处,这样就不会弄乱循环并显示正确的图像。
有关正确操作的建议吗?
答案 0 :(得分:0)
has_post_thumbnail()
接受帖子ID作为其参数,改为使用has_post_thumbnail($post->ID)
。因为您使用的是修改后的循环,所以需要隐式地将post ID作为函数参数传递。
答案 1 :(得分:0)
虽然我现在无法确定,但我的猜测是,通过global $post;
获取ID,您将获得第一篇文章的ID。
由于您处于循环中,您可以使用以下函数解决此问题:
get_the_ID();
(见WordPress Function reference)
这将为您提供以下获取图像ID的方式:
$imgID = get_post_thumbnail_id(get_the_ID()); //get the id of the featured image