将上传的图像的URL转换为自定义帖子类型的问题

问题描述 投票:0回答:1

我有一个名为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的任何图像。您能告诉我我在想什么,做错什么吗?

wordpress wordpress-theming
1个回答
0
投票

请用您的功能代替。

<?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();

?>
© www.soinside.com 2019 - 2024. All rights reserved.