我为分类 = 帖子类别中的图像创建了一个 ACF 字段。我写了一个循环来输出这些图像和作品。我在帖子底部添加了这个作为参考。
现在我想选择图像大小,所以我尝试了使用 wp_get_attachment_image 的更高级的方法(此处概述:http: //www.advancedcustomfields.com/resources/field-types/image/):
$attachment_id = get_field('field_name');
$size = "full"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
所以我把上面的改成
$attachment_id = get_field('category_image', $taxonomy . '_' . $term->term_id);
$size = "full"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
并在图像中添加
echo $image[0]
但这会输出 bool(false) 并且不起作用。有任何想法吗?
这是在循环中正确输出图像 url 的代码。
<div class="category-image">
<?php
$taxonomy = 'category';
$queried_term = get_term_by( 'slug', get_query_var($taxonomy), 0 );
$terms = get_terms($taxonomy);
if ($terms) {
echo '<ul>';
foreach($terms as $term) {
// ACF
$image = get_field('category_image', $taxonomy . '_' . $term->term_id);
// TEST to see field
// var_dump( $image );
if( get_field('category_image', $taxonomy . '_' . $term->term_id)):
?> <li><a href="<?php echo get_term_link($term->slug, $taxonomy) ?>"><img src="<?php echo $image['url'] ; ?>" alt="" /><h4><?php echo $term->name ?></h4></a></li>
<?php endif; } ?>
</ul> </div>