PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 10, 2022

[FIXED] How to add ACF PRO image src="" & alt="" in echo for product category

 May 10, 2022     advanced-custom-fields, archive, php, product, wordpress     No comments   

Issue

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.


Solution

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' );
}
?>


Answered By - Bubble
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing