目前在我的php文件顶部有以下内容
$post1->ID = 85;
$post2->ID = 87;
$post3->ID = 89;
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post1->ID ), 'single-post-thumbnail' );
$image2 = wp_get_attachment_image_src( get_post_thumbnail_id( $post2->ID ), 'single-post-thumbnail' );
$image3 = wp_get_attachment_image_src( get_post_thumbnail_id( $post3->ID ), 'single-post-thumbnail' );
然后在我的文件中进一步向下输出图像的URL
style="background-image: url('<?php echo $image[0]; ?>')"
style="background-image: url('<?php echo $image2[0]; ?>')"
style="background-image: url('<?php echo $image3[0]; ?>')"
很多重复的代码,但我不确定如何将post id放在数组中。
将数组设置为
是否正确$post->ID = array(85, 87, 89);
但我不确定其余部分。
答案 0 :(得分:1)
你可以说:
$post_array = array(85, 87, 95);
foreach($post_array as $p)
{
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $p ), 'single-post-thumbnail' );
/* enter code here to output HMTL element use $img to set BG img */
}
答案 1 :(得分:0)
我看了你的代码,我就是这样做的:
<?php
//An array to store your ID's
$post->ID = array(85, 87, 89);
//For loop to start counting from zero
//For i = 0; When i is smaller or exactly the number of elements in the ID-array; Increment i by one
for($i=0; $i<=count($post->id); $i++){
//Creating the image-array
$image[] = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID[$i] ), 'single-post-thumbnail' );
}
//And then looping the final $image -array:
//For i = 0; When i is smaller or exactly the number of elements in the image-array; Increment i by one
for($i=0; $i<count($image); $i++){
//Where we echo out the final result through a for-loop:
echo 'style="background-image: url('{$image[$i]}')"';
}
通过使用循环和数组,您可以使代码更加动态。