我有一个名为CPT的书籍,可以像这样循环遍历,但是我需要将上传图像的URL放入帖子中(不在Gallery中)。我试图将WP wp_get_attachment_image_src()
用作波纹管,但是我不知道我应该为$attachment_id
传递什么,因为它是必需的
<?php
$loop = new WP_Query(array(
'post_type' => 'books',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'romance'
)
)
)
);
while ($loop->have_posts()):
$loop->the_post();
$image_attributes = wp_get_attachment_image_src('', 'full' );
if ( $image_attributes ) : ?>
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
<?php endif;
endwhile;
wp_reset_query();
?>
显然,这不会返回上传到CPT URL的任何图像。您能告诉我我在想什么,做错什么吗?
请用您的功能代替。
<?php
$loop = new WP_Query(array(
'post_type' => 'books',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'romance'
)
)
)
);
while ($loop->have_posts()):
$loop->the_post();
//used "get_post_thumbnail_id($post->ID)" perameter
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full' );
if ( $image_attributes ) : ?>
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
<?php endif;
endwhile;
wp_reset_query();
?>