Support

Account

Home Forums General Issues want to display Category Image in loop

Solved

want to display Category Image in loop

  • Hello, i added custom filed for category, called cat_thumbnail_image i want to show my category.php child category information with thumbnail images. i already search in this forum and got many post like it tried almost all the way but failed to get result. i am sure i am missing something.

    here is code

    Return Value is Image URL

    <?php
    $args = array('child_of' => 43);
    $categories = get_categories( $args );
    
    foreach($categories as $category)
    $image = get_image('product_categories_'.$category->term_id);
     { 
    	echo '<figure class="snip0015"><img src="'.$image['url'].'" alt="'.$image['alt'].'" /><h2>'. $category->name.'</h2>';
        echo '<div> Description:'. $category->description . '</div>';
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '></a></figcaption></figure>';  
    }
    ?>

    it show me Fatal error: Call to undefined function get_image() …..

  • i tried
    $image=get_image('cat_thumbnail_image'.$category->term_id); >> error showing
    and
    $image=get_field('cat_thumbnail_image', 'category_'.$category->term_id); >> image filed empty
    and
    $image=get_field('cat_thumbnail_image', $category); >> image filed empty

  • Hi @pagol

    Could you please tell me where did you add the custom field? Was it the product category or the normal post category?

    ACF doesn’t have a function called get_image(), that’s why you got the error message. Could you please try to set the ID manually like below?

    $image = get_field('cat_thumbnail_image', 'category_99');
    print_r($image);

    Where ’99’ is the ID of the category. If it works, then there’s something wrong with your $category variable.

    Thanks 🙂

  • This is filed section screenshot

    This is category section screenshot

    i can not put static category id. because under my category i have more sub category, in my query i wan to show all child category title, description and image

  • Hi @pagol

    Could you please test the following code for me?

    $image = get_field('cat_thumbnail_image', 'category_99');
    echo <pre>;
    
    echo "Image field value:";
    var_dump($image);
    
    echo "Category value:";
    var_dump($category);
    
    echo </pre>;

    Where ’99’ is the ID of the category. Yes, please set the ID manually in this test so we know if the get_field() function returns the value or not.

    Thanks 🙂

  • it show long dump code i just paste part

    <?php
    $args = array('child_of' => 43);
    $categories = get_categories( $args );
    
    foreach($categories as $category)
    
     { 
    	
    $image = get_field('cat_thumbnail_image', '47');
    echo '<pre>';
    
    echo "Image field value:";
    var_dump($image);
    
    echo "Category value:";
    var_dump($category);
    
    echo '</pre>';
    }
    
    ?>

    47 is my one of category id.

    result bellow

    Image field value:NULL Category value:object(WP_Term)#399 (17) { ["term_id"]=> int(47) ["name"]=> string(7) "Banners" ["slug"]=> string(14) "banner-designs" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(47) ["taxonomy"]=> string(8) "category" ["description"]=> string(124) "We design and print custom size banners for all industries including motorsports, transportation, special events & more." ["parent"]=>

  • Hi @pagol

    How about this one?

    $image = get_field('cat_thumbnail_image', $category->taxonomy . '_' . $category->term_id );
    echo '<pre>';
    
    echo "Image field value:";
    var_dump($image);
    
    echo '</pre>';

    This page should give you more idea about it: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/.

    Thanks!

  • COOL… this is work. 🙂 thanks you so much for quick and great support ..

  • now i am facing one small issue. bellow is my code. it is working fine. i get proper value but issue i want only active category value. bellow code show all category image loop

    <?php
    $args = array('child_of' => 67);
    $categories = get_categories( $args );
    foreach($categories as $category)
     { 
    	$image = get_field('cat_thumbnail_image', $category->taxonomy . '_' . $category->term_id );
    	
    	echo '<div class="test">'.$image.'</div>';
    }
    ?>
  • Hi @pagol

    Could you please tell me what did you mean by “only active category value”? Did you mean only show categories that have the “cat_thumbnail_image” field value? If that’s the case, I believe you can do it like this:

    foreach($categories as $category){ 
        
    	$image = get_field('cat_thumbnail_image', $category->taxonomy . '_' . $category->term_id );
        
        // Check if the image exists
        if( $image ){
            echo '<div class="test">'.$image.'</div>';
        }
    	
    }

    If that is not what you want, could you please explain it to me again?

    Thanks 🙂

  • Thanks for reply.

    actually before code was loop. meaning all child category image will show. that was fine work.

    but now i need also only show category image. not loop all child category.

    example my category like bellow

    • Printing (main category)
      • – 4 color (child category)
      • – 2 color (child category)
      • – offset print (child category)

    so now when i will view/access 4 color category (cat-printing.php this page for all printing child category template ) then in that page will show 4 color category image only

    i tried you code not work really . it show error foreach($categories as $category){

  • Hi @pagol

    I’m not sure if I understand your explanation, so please forgive me if I’m wrong.

    To show the image for the child category, I believe you can do it like this:

    $current_term = get_queried_object(); 
    
    $image = get_field('cat_thumbnail_image', $current_term );
    	
    echo '<div class="test">'.$image.'</div>';

    If that is not what you want, could you please share some screenshots of the issue with notes on how you want it to be?

    Thanks 🙂

  • wow… cool !!!!! @James … it work super 🙂 thank you so much.

    two issue solve it here 🙂

    Thanks again for great and quick support

  • Hi James, I saw this topic and I am facing the same issue as some others here. Maybe you can help me out too, here 😉

    I have the following code:

    <?php
    
                        foreach ( $terms as $term ) {
    
                       $image = get_field('berufe-thumb', $term->taxonomy . '_' . $term->term_id );
                        echo '<pre>';
    
                        echo "Image field value:";
                        var_dump($image);
    
                        echo '</pre>';
                         
    
                ?>

    The result is: Image field value:NULL

    Could you have a look at it, please?

Viewing 14 posts - 1 through 14 (of 14 total)

The topic ‘want to display Category Image in loop’ is closed to new replies.