0

我創建了一個產品立柱型WordPress的所謂產品,用先進的自定義字段(ACF)被稱爲「product_categories」的自定義分類我添加了一個像場的分類法可以添加一個圖像到每個產品類別。以下是即時查詢。它工作正常,但是我想以自定義大小顯示圖像,所以它不會每次加載巨大的2000x3000圖像。這裏是我當前的代碼:WordPress的自定義分類圖像的大小調整ACF

<?php 

$prodargs=array( 
    'hide_empty'  => 0, 
    'parent'  => 0, 
    'taxonomy'  => 'product_categories'); 

    $prodcats=get_categories($prodargs); 

    foreach($prodcats as $pc){ 
     $termlink = get_term_link($pc->slug, 'product_categories'); 

?> 

    <a class="single-library-cat" href="<?php echo $termlink; ?>"> 
     <img src="<?php the_field('taxonomy_image', 'product_categories_'.$pc->term_id); ?>" /> 
     <?php echo $pc->name; ?> 
    </a> 

<?php } ?> 

我通過ACF知道我可以訪問額外的圖像數據,如尺寸,寬度高度。只是不知道我會執行它到我的正確的代碼。 https://www.advancedcustomfields.com/resources/image/

回答

0

1)的functions.php與add_image_size函數定義自定義尺寸和添加輔助方法

add_theme_support('post-thumbnails'); 
add_image_size('category', 250, 200, true); 

function get_src_picture($id, $size) 
{ 
    $thumb = wp_get_attachment_image_src($id, $size); 
    return $thumb[0]; 
} 

2)改變 「返回值」 ACF場的圖像的屬性設置爲 「圖像ID」 enter image description here

3)獲取圖像拇指

<img src="<?= get_src_picture(get_field('taxonomy_image', 'product_categories_'.$pc->term_id), 'category') ?>" /> 
+0

感謝您的幫助。我也跟着你的腳步,並即時得到這個錯誤... 解析錯誤:'。$ MC-> term_id),「語法錯誤,意外」(T_CONSTANT_ENCAPSED_STRING) –

+0

我加了一個「;」到圖像縮略圖的末尾,但沒有解決它。 –

+0

你使用'$ MC-> term_id'或'$ PC-> term_id'?在你的foreach循環是'$ pc' – Grin

相關問題