ACF从分类法输出的图像大小

时间:2013-10-29 11:33:42

标签: wordpress-theming wordpress advanced-custom-fields

我为taxonomy = post category中的图像创建了一个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)并且不起作用。有什么想法吗?

以下是在循环中正确输出图片网址的代码。

<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>

1 个答案:

答案 0 :(得分: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
    ));

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