duanlan7239 2018-07-03 14:42
浏览 91
已采纳

如何在echo中为产品类别添加ACF PRO图像src =“”&alt =“”

I'm struggling to add ACF PRO get_field for image in echo for product categories to display in 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' );
}
?>

Does anyone know about this?

Thanks,

Shaun.

  • 写回答

1条回答 默认 最新

  • doujiao6507 2018-07-18 09:14
    关注

    RE: How do placeholder image in img src if no image in category?

    This works for me:

    <?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' );
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上