Support

Account

Home Forums General Issues Displaying image in WooCommerce product category.

Solved

Displaying image in WooCommerce product category.

  • Hi, I’m trying to display an image in the product category, I have this code in my functions.php:

    if ( is_product_category() ) {
    		// vars
    		$queried_object = get_queried_object(); 
    		$taxonomy = $queried_object->taxonomy;
    		$term_id = $queried_object->term_id;  
    		$foto = get_field( 'foto', $queried_object );
    		$foto = get_field( 'foto', $taxonomy . '_' . $term_id );
    		if( get_field('foto') ) {
    			echo '<div class="cat-banner"><img src="' . $foto . '"/></div>';
    		}
    	}

    My ACF field is set to IMAGE URL, but I also tried IMAGE OBJECT with no success.

    I appreciate any guide with this.

    Thanks.

  • Hi,

    Actually, if you want to use get_field() method outside a WP loop, then you have to specify the $post_id as a second argument in the method get_field( $fieldname, $post_id).

  • Ok, just $post_id or that should be replaced with some specific data?

    Thanks!

  • actually, what you have in your code here, either of them, should work if the image is added to the category.

    
    $foto = get_field( 'foto', $queried_object );
    $foto = get_field( 'foto', $taxonomy . '_' . $term_id );
    

    but this will be false since it’s not using one of the two values for post ID from above

    
    if( get_field('foto') ) {
    

    pick one of the first two and use it for all acf calls

  • That worked, in case this helps somebody else:

    if ( is_product_category() ) {
    		// vars
    		$queried_object = get_queried_object(); 
    		$taxonomy = $queried_object->taxonomy;
    		$term_id = $queried_object->term_id;  
    		$foto = get_field( 'foto', $queried_object );
    		$foto = get_field( 'foto', $taxonomy . '_' . $term_id );
    		if( get_field('foto', $taxonomy . '_' . $term_id) ) {
    			echo '<div class="cat-banner"><img src="' . $foto . '"/></div>';
    		}
    	}
  • Hello maira,
    I am trying to display the image but its not. is there any other settings?

    Thanks

  • Hi there,

    Make sure your field name is $foto or replace that part of the code with your own field name.

  • Thanks for reply maira,
    Yes the field name is $foto. but its not working.
    Actually i want to add a label on the Product category images like this
    http://prntscr.com/gefwfd

    I have set the setting below way…

    1. Set file name $foto

    2. Return Value set to Image URL http://prntscr.com/geft8t

    3. set rule, Taxonomy Term -> is equal to -> Product Categories
    http://prntscr.com/gefrjw

    4. The Image showing ok on the product category fields http://prntscr.com/geg1s2

    5. added your code to functions.php

    if ( is_product_category() ) {
    // vars
    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;
    $foto = get_field( 'foto', $queried_object );
    $foto = get_field( 'foto', $taxonomy . '_' . $term_id );
    if( get_field('foto', $taxonomy . '_' . $term_id) ) {
    echo '<div class="cat-banner"></div>';
    }
    }

    i am a newbee, Sorry for bothering you. Would you like to suggest me what else need to do.

    Thanks in advance

  • Oh ok, then this code won’t work because what this does is to display a banner image in the product Category page, as in website-url/product-category/computers, is different to what you want to do.

    In this case, I’d use a plugin to add custom badges, I’ve used this one and it works ok.

  • ohh i see, i will check it
    anyway Thanks..

  • Hi Maira,

    I want to add different feature image for different product category. I am using Maisha theme.

    I used your code but it is not working. I am very new to PHP. And i just used your code as it is. Can you please help me figure out what I am doing wrong.

    if ( is_product_category() ) {
    // vars
    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;
    $foto = get_field( ‘foto’, $queried_object );
    $foto = get_field( ‘foto’, $taxonomy . ‘_’ . $term_id );
    if( get_field(‘foto’, $taxonomy . ‘_’ . $term_id) ) {
    echo ‘<div class=”cat-banner”></div>’;
    }
    }

    I did not get field foto. Could you please explain it more. In previous post you said change it or make sure field name is same . What field name are we talking about here.

  • Sorry I forgot to mention that iIts a word press site and I have created a child theme.

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

The topic ‘Displaying image in WooCommerce product category.’ is closed to new replies.