0

我为分类 = 帖子类别中的图像创建了一个 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>
4

1 回答 1

0

看起来您的图像字段返回了一个对象(这很好),这意味着您可以通过直接从返回的 Image 对象中拉取所需大小的 Url 来非常轻松地解决这个问题,如下所示:

<img src="<?php echo $image['sizes']['large'] ; ?>" alt="" />

返回的对象get_field('category_image')如下所示:

Array(
[id] => 540
[alt] => A Movie
[title] => Movie Poster: UP
[caption] => sweet image
[description] => a man and a baloon
[url] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
[sizes] => Array
    (
        [thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-150x150.jpg
        [medium] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-300x119.jpg
        [large] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
        [post-thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
        [large-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
        [small-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-500x199.jpg
    ));

来源:http : //www.advancedcustomfields.com/resources/field-types/image/#template-usage

于 2013-11-08T21:38:53.973 回答