Ask your WordPress questions! Pay money and get answers fast! (more info)

add unique id to a category image

  • SOLVED

I am using this code to call the category image at the top of a product page in woo commerce - single product.

<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';
}
?>


The problem comes with the code when the product is in more than one category. This results in multiple images being called.

I need to add a unique id to each image so I can turn them off via css

Another alternative would be an - IF statement

If category is ??? get this image.

Just not sure how to write it.

Answers (1)

2016-04-06

Reigel Gallarde answers:

changing your echo to something like:

echo '<img id="image-term-'.$term->term_id.'" src="'.$image.'">';

would give each image a unique id based on the term_id..

if you want also only one image, you can forget the foreach loop and use something like:


$terms = get_the_terms( $post->ID, 'product_cat' );

$term = $terms[0];
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';


Amanda comments:

that's great, both work

first option is adding an id but it is ignoring my css and won't turn it off.

the second option - this one doesn't bring up the image I need, only the image I wan't to get rid of


Reigel Gallarde comments:

if you have specific name for example, you can do like this


foreach ( $terms as $term ){
if ($term->name == 'my_preferred_category') { // change my_preferred_category
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';
}
}


Amanda comments:

i don't think that one will work because I don't have just one image I want to apply to all categories.

One product falls into two categories and those two category images show on this products page.

I think with this one it is pulling the categories in alphabetical order

$terms = get_the_terms( $post->ID, 'product_cat' );
$term = $terms[0];
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';


Can I get it to display them in descending order maybe?


Reigel Gallarde comments:

you want the last one?


Amanda comments:

the last one didn't work for me


Reigel Gallarde comments:

I mean, why do you want it descending...

another approach also is if you know the id... instead of $term->name, use $term->term_id

if ($term->term_id == 123 ) { // 123 is the term id


Amanda comments:

with this line
$term = $terms[0];
it tells it to grab the first category image.

If I change it to - 1, i get the right image for the ones I need but the rest of the products don't have an image.

my category ID:20 (for the image I need)
products that need to use this image = ID: 43


For this
if ($term->term_id == 123 ) { // 123 is the term id

Would I have to write each product out?


Reigel Gallarde comments:

foreach ( $terms as $term ){

if ($term->term_id == 20) { // change category id
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';
}

}


Amanda comments:

that's not showing any image now. I tried it with a few category ids.


Reigel Gallarde comments:

I forgot add this, did you have it?
$terms = get_the_terms( $post->ID, 'product_cat' );


Reigel Gallarde comments:

you can also use this directly without the foreach,

$category_thumbnail = get_woocommerce_term_meta(20, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';


this will get the image of product_cat term id 20


Amanda comments:

That's not going to work because I can't have different templates.

I am amending the woocoomerce.php file in my theme.

At the top I am using the code to pull the category image.
This needs to pull the image for the category that relates to the product being displayed.

I can use conditional tags maybe

https://docs.woothemes.com/document/conditional-tags/



The below code doesn't work but you will get the idea of what I need to do

<?php
if ( is_product() ) {

if ( is_product() ) {
$terms = get_the_terms( $post->ID, 'product_cat' );
$term = $terms[0];
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img class="replacement" src="'.$image.'">';

} elseif ( is_product( 'ice-gel' ) ) {
echo '<img class="replacement" src="http://www???????">';
}

}
?>


Reigel Gallarde comments:

is ice-gel a product or a category?


Amanda comments:

product


Amanda comments:

I have been given this suggestion -

Rather than grab the first one with:
$term = $terms[0]
Loop over the $terms with a foreach loop.


Reigel Gallarde comments:

you have mentioned this a few days back,

<blockquote>my category ID:20 (for the image I need)
products that need to use this image = ID: 43</blockquote>

can you explain more?


Amanda comments:

there is only one product in the shop that is under two categories. But the client only wants the category ID:20 image to show up for this product not the other category image.

Product ID 43
Category ID 20


Reigel Gallarde comments:

<?php

if ( is_product() ) {
global $post;

if ( $post->ID == 43 ) {

$term = get_term( 20, 'product_cat' );

} else {

$terms = get_the_terms( $post->ID, 'product_cat' );
$term = $terms[0];

}

if ($term) {
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img class="replacement" src="'.$image.'">';
}
}
?>


Amanda comments:

that's it, thank you so much.
Working Perfectly.