0

I am using a hook to change the appearance of categories on my website.

my problem is, it works fine for product categories but when i decided to use a custom taxonomy for the purpose, they are appearing but without their image ( i set an image field up with ACF for the custom tax ; return format image array ) it is the custom tax term image that is missing whereas everything else works ( the custom tax link, and name are showing )

I have no idea where I am going wrong with it, i have tried endless amounts of different things, it cannot find the thumbnail ID of the custom taxonomy.

Can anybody give me some advice please.

if ( ! function_exists('custom_subcategory_thumbnail') ):

        remove_action('woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail');

        add_action('woocommerce_before_subcategory_title', 'custom_subcategory_thumbnail', 10, 1);

        function custom_subcategory_thumbnail( $category ) {

    

            $thumbnail_id           = get_term_meta( $category->term_id, 'thumbnail_id', true );
            
        /*    echo '<pre>';
            var_dump($testing);
            echo '</pre>';*/
        //    var_dump($thumbnail_id);

            
          //  $taxonomy = $queried_object->taxonomy;
           // $wpimage = get_field('workplace_thumb', 'workplaces' . '_' . $category->term_id);
          /*  echo '<pre>';
            print_r($thumbnail_id);echo '</pre>';*/
           
         //   $attachment_id = get_field( 'workplace_thumb', 'workplaces_' . $category->term_id );
    
/*
            echo '<pre>';
            print_r($attachment_id);
            echo '</pre>';*/
            if ( $thumbnail_id ) {

                $image        = wp_get_attachment_image_src( $thumbnail_id, 'woocommerce_single' );

                $image        = $image[0];

                $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, 'shop_single_image_size' ) : false;

                $image_sizes  = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, 'shop_single_image_size' ) : false;

            }
            
            
            else{

                $image        = wc_placeholder_img_src();

                $image_srcset = $image_sizes = false;

            }

    

            if ( $image ) {

                // Prevent esc_url from breaking spaces in urls for image embeds

                // Ref: https://core.trac.wordpress.org/ticket/23605

                $image = str_replace( ' ', '%20', $image );

    

                echo '<div class="banner-box__image brand-grayscale">
                <a href="' . esc_url( get_term_link( $category, 'product_cat' ) ) . '" >
                <span class=" woocommerce-loop-category__thumbnail brand-grayscale" style="background-image :url(' . esc_url( $image ) . ');"></span></a>

            </div>';

            }

        }

        endif;

1 Answer 1

0

I figured it out, in case anyone else needs it,

it was really simple, and just used an elseif for when the id was not found & changed the custom field name to the custom taxonomy image field ( workplace_thumb ) for that taxonomy id.

  if ( ! function_exists('custom_subcategory_thumbnail') ):

        remove_action('woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail');

        add_action('woocommerce_before_subcategory_title', 'custom_subcategory_thumbnail', 10, 1);

        function custom_subcategory_thumbnail( $category ) {

    

            $thumbnail_id           = get_term_meta( $category->term_id, 'thumbnail_id', true );
           /* $term = get_field('workplace_thumb', 'workplace_' . $category->post_ID);


            echo '<pre>';
            echo "thumbnail id variable value vardump: ";echo '<br />';
            var_dump($thumbnail_id); echo '<br />';
            echo 'term variable value: ';echo '<br />';
           var_dump($term);echo '<br />';
           echo "Category value:";echo '<br />';
           var_dump($category);echo '<br />';
           echo '</pre>';*/
            
          //  $taxonomy = $queried_object->taxonomy;
           // $wpimage = get_field('workplace_thumb', 'workplaces' . '_' . $category->term_id);
          /*  echo '<pre>';
            print_r($thumbnail_id);echo '</pre>';*/
           
         //   $attachment_id = get_field( 'workplace_thumb', 'workplaces_' . $category->term_id );
    
/*
            echo '<pre>';
            print_r($attachment_id);
            echo '</pre>';*/
            if ( $thumbnail_id ) {

                $image        = wp_get_attachment_image_src( $thumbnail_id, 'woocommerce_single' );

                $image        = $image[0];

                $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, 'shop_single_image_size' ) : false;

                $image_sizes  = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, 'shop_single_image_size' ) : false;

            }elseif ( ! $thumbnail_id ){
                $thumbnail_id_workplace             = get_term_meta( $category->term_id, 'workplace_thumb', true );
                $image        = wp_get_attachment_image_src( $thumbnail_id_workplace, 'woocommerce_single' );

                $image        = $image[0];

                $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id_workplace, 'shop_single_image_size' ) : false;

                $image_sizes  = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id_workplace, 'shop_single_image_size' ) : false;
            }
          
            
            else{

                $image        = wc_placeholder_img_src();

                $image_srcset = $image_sizes = false;

            }

    

            if ( $image ) {

                // Prevent esc_url from breaking spaces in urls for image embeds

                // Ref: https://core.trac.wordpress.org/ticket/23605

                $image = str_replace( ' ', '%20', $image );

    

                echo '<div class="banner-box__image brand-grayscale">
                <a href="' . esc_url( get_term_link( $category, 'product_cat' ) ) . '" >
                <span class=" woocommerce-loop-category__thumbnail brand-grayscale" style="background-image :url(' . esc_url( $image ) . ');"></span></a>

            </div>';

            }

        }

        endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.