如何在产品类别的回显中添加ACF PRO图像src =“”和alt =“”

时间:2018-07-03 14:42:59

标签: php wordpress advanced-custom-fields product archive

我正在努力为回显中的图像添加ACF PRO get_field,以便将产品类别显示在Archive-product.php中

<?php
$orderby = 'name';
$order = 'asc';
$hide_empty = true ;
$cat_args = array(
'orderby'    => $orderby,
'order'      => $order,
'hide_empty' => $hide_empty,
);

$product_categories = get_terms( 'product_category', $cat_args );

if( !empty($product_categories) ){
echo '<div class="container">';
echo '<div class="row">';
foreach ($product_categories as $key => $category) {
    $image = get_field('product_category' . $term_id );
    echo '<div class="col-lg-4">';
    echo '<a href="'.get_term_link($category).'" >';
    echo $category->name;
    echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
    echo '</a>';
    echo '</div>';
}
echo '</div>';
echo '</div>';
}
else {
// no posts found
echo wpautop( 'Sorry, no posts were found' );
}
?>

有人知道吗?

谢谢

肖恩。

1 个答案:

答案 0 :(得分:0)

RE:如果类别中没有图像,如何在img src中显示占位符图像?

这对我有用:

<?php

$orderby = 'name';
$order = 'asc';
$hide_empty = true ;
$cat_args = array(
'orderby'    => $orderby,
'order'      => $order,
'hide_empty' => $hide_empty,
);

$product_categories = get_terms( 'product_category', $cat_args );

if( !empty($product_categories) ){
    echo '<div class="container">';
    echo '<div class="row">';
    foreach ($product_categories as $key => $category) {
        echo '<div class="col-lg-4">';
        echo '<a href="'.get_term_link($category).'" >';
        echo $category->name;
        $image = get_field('product_category', $category );
        if($image) {
        echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
        } else {
            echo '<img src="/wp-content/uploads/2018/04/placeholder.png">';
        }
        echo '</a>';
        echo $category->description;
        echo '</div>';
    }
    echo '</div>';
    echo '</div>';
}
else {
    // no posts found
    echo wpautop( 'Sorry, no posts were found' );
}
?>