Get thumbnail from Woocommerce Tag - php

I am using this plugin
https://woocommerce.com/products/custom-product-tag-image/
To add images to my tags.
I'd like to display a list of tags with images. I've tried so many approaches but nothing yet.
Here's my code which outputs a list of tags but no images.
<?php $taxonomyName = "product_tag";
$terms = get_terms(array('taxonomy' => 'product_tag', 'hide_empty' => false)); ?>
<?php foreach ( $terms as $term ) { ?>
<div class="item" >
<?php
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$term_img = wp_get_attachment_url( $thumb_id );
?>
<img src="<?php echo $term_img; ?>" />
<?php echo $term->name; ?>
</div>
<?php } ?>
Suggestions?

You can use WC get_woocommerce_term_meta() and WP wp_get_attachment_url(). check below code.
<?php foreach ( $terms as $term ) { ?>
<div class="item">
<?php
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$term_img = wp_get_attachment_url( $thumb_id );
?>
<?php echo $term->name; ?>
</div>
<?php } ?>

The code below is what worked for me as woocommerce_get_term_meta is deprecated and thumbnail_id doesn't exist anymore:
<?php $taxonomyName = "product_tag";
$terms = get_terms(array('taxonomy' => 'product_tag', 'hide_empty' => false)); ?>
<?php foreach ( $terms as $term ) { ?>
<div class="item" >
<?php
$term_image_id = get_term_meta( $term->term_id, 'product_search_image_id', true );
$term_image = wp_get_attachment_url( $term_image_id );
?>
<img src="<?php echo $term_image; ?>" />
<?php echo $term->name; ?>
</div>
<?php } ?>

Related

How to get the category

There is a cycle that outputs products
<?php
$args = array('post_type' =>
'product');
$loop = new WP_Query($args);
foreach ($loop->posts as $have_post) : ?>
<div class="swiper-slide">
<a href="
<?php
$permalink = get_post_permalink($have_post->ID);
echo $permalink;
?>
" class="goods__item">
<div class="goods__text">
<?php
echo $have_post->name; ?>
</div>
<div class="goods__img">
<?php
echo get_the_post_thumbnail($have_post->ID, 'full', array('alt' => $have_post->post_title, 'data-product_id' => $have_post->ID)); ?>
</div>
<div class="goods__name"><?php echo $have_post->post_title; ?></div>
<div class="goods__price">
<?php
$price = get_post_meta($have_post->ID, '_price', true);
echo $price; ?> руб.
</div>
</a>
</div>
<?php endforeach; ?>
Need to show the category of current product here. I cant get the category. Please help
<div class="goods__text"><?php echo $have_post->name; ?></div>
You could use get_the_terms to get the correct terms. I believe it's product_cat the correct taxonomy.
$terms = get_the_terms( $have_post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
$product_cat_name = get_cat_name($product_cat_id);
break;
}
Try get_the_category( $post->ID );

WP_Query posts get term list outside of loop?

Maybe I'm going about this incorrectly but I'm having an issue with getting info outside of the while loop:
<?php
$title = get_field('car_list_title');
$field = get_field('tax_field_selector');
$query = new WP_Query( array(
'post_type' => 'cars',
'taxonomy' =>'make',
'term' => $field->name,
'posts_per_page' => -1,
'orderby' =>'title',
'order' =>'ASC'
) );
$taxonomy = get_terms( array(
'taxonomy' => 'location',
'hide_empty' => true
) );
if ( $field || $query->have_posts() ) :
?>
<div class="c-cars">
<h2 class="c-cars_title u-t--underline--lightblue">
<?= $title; ?>
</h2>
<?php foreach( $taxonomy as $tax ) :
$tax_name = $tax->name;
?>
<div class="c-cars_row">
<h4 class="c-cars_location-title">
<?= $tax_name; ?>
</h4>
<div class="c-cars_cars">
<?php while ( $query->have_posts() ) : $query->the_post();
$title = get_the_title();
$link = get_permalink();
$image = get_field('car-picture');
$image_alt = get_field('car_alt');
$image_title = get_field('car_title');
$post_id = get_the_ID();
$terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true));
$location = $terms[0]->name;
?>
<?php if( $location === $tax_name ) : ?>
<div class="c-cars_car">
<a href="<?= $link; ?>">
<img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>">
</a>
<h4 class="text-center">
<a href="<?= $link; ?>">
<?= $title; ?>
</a>
</h4>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
So what happens here is I get a list of the locations and all the cars in those locations:
Location 1:
Car
Car
Car
Location 2:
Car
Car
Car
Location 3:
Location 4:
Car
Car
Car
The problem here is, as an example, Location 3 shows up even though there's no "posts" in that term.
The while loop is only cars of a specific model, sorted into what location they are at.
I'm not really sure how to filter out the empty locations.
I do:
<?php if( $location === $tax_name ) : ?>
Inside of the loop and that filters them out of the locations but still leaves the location title because it's outside of the while loop. If I were able to do this earlier up in the code it may work but I can't get the list of active terms outside of the while loop.
I'm kind of lost right now. Any ideas or suggestions? Thanks!
you can check using condition if has a term so show title either it will be blank try it below condition and mentioned in a comment if it's work or not.
has_terms
https://developer.wordpress.org/reference/functions/has_term/
function check if the post has terms.
if( has_term( $location, $tax_name ) ) {
// do something
}
Okay, I have updated your answer please try it below code. I have just get post count of terms and applied condition if terms has post count so show terms title name or if there is no post count of terms so the title will show blank.
<?php
$title = get_field('car_list_title');
$field = get_field('tax_field_selector');
$query = new WP_Query( array(
'post_type' => 'cars',
'taxonomy' =>'make',
'term' => $field->name,
'posts_per_page' => -1,
'orderby' =>'title',
'order' =>'ASC'
) );
$taxonomy = get_terms( array(
'taxonomy' => 'location',
'hide_empty' => true
) );
if ( $field || $query->have_posts() ) :
?>
<div class="c-cars">
<h2 class="c-cars_title u-t--underline--lightblue">
<?= $title; ?>
</h2>
<?php foreach( $taxonomy as $tax ) :
$tax_name = $tax->name;
$tax_post_count = $tax->count;
?>
<div class="c-cars_row">
if ( $tax_post_count > 0 ) : ?>
<h4 class="c-cars_location-title">
<?= $tax_name; ?>
</h4> <?php
else : ?>
<h4 class="c-cars_location-title">
<?= $tax_name = ''; ?>
</h4> <?php
endif; ?>
<div class="c-cars_cars">
<?php while ( $query->have_posts() ) : $query->the_post();
$title = get_the_title();
$link = get_permalink();
$image = get_field('car-picture');
$image_alt = get_field('car_alt');
$image_title = get_field('car_title');
$post_id = get_the_ID();
$terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true));
$location = $terms[0]->name;
?>
<?php if( $location === $tax_name ) : ?>
<div class="c-cars_car">
<a href="<?= $link; ?>">
<img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>">
</a>
<h4 class="text-center">
<a href="<?= $link; ?>">
<?= $title; ?>
</a>
</h4>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>

How to change category thumbnail size in WooCommerce

With the following code I managed to display my featured categories title, description and thumbnail. When I uploaded the thumbnail, it was 500 * 500 in its dimensions. But when I visit the page I see the thumbnail being cropped as 150 * 150.
<?php
$args_t = array(
'taxonomy' => 'product_cat',
'include' => array( 16, 15, 17 ),
'orderby' => 'meta_value',
);
$thirds_categories = get_categories( $args_t );
foreach ( $thirds_categories as $cat ) {
if( $cat->category == 0 ) {
$cat_class = mb_strtolower($cat->name);
$image = wp_get_attachment_url( $thumbnail_id );
$cat_thumb_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
$term_link = get_term_link( $cat, 'product_cat' );?>
<div class="categories_box">
<a href="<?php echo $term_link; ?>">
<img src="<?php echo $cat_thumb_url; ?>" alt="<?php echo $cat->name; ?>" />
<h4> <?php echo $cat->name; ?> </h4>
<p><?php echo $cat->description; ?> </p>
<button>View Products</button>
</a>
</div>
<?php }
} wp_reset_query();
?>
How can I change the size of my categorythumbnails?
It is my first time doing a WooCommerce site.
You should use wp_get_attachment_image_src(); for getting different sizes of image. This will return you an array with URL, width, height and cropping mode of this image.
Have a try
$args_t = array(
'taxonomy' => 'product_cat',
'include' => array( 16, 15, 17 ),
'orderby' => 'meta_value',
);
$thirds_categories = get_categories( $args_t );
foreach ( $thirds_categories as $cat ) {
if( $cat->category == 0 ) {
$cat_class = mb_strtolower($cat->name);
$image = wp_get_attachment_url( $thumbnail_id );
$cat_thumb_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
$term_link = get_term_link( $cat, 'product_cat' );
$thmb = wp_get_attachment_image_src($cat_thumb_id, 'medium');
?>
<div class="categories_box">
<a href="<?php echo $term_link; ?>">
<img src="<?php echo $thmb[0]; ?>" alt="<?php echo $cat->name; ?>" />
<h4> <?php echo $cat->name; ?> </h4>
<p><?php echo $cat->description; ?> </p>
<button>View Products</button>
</a>
</div>
<?php }
} wp_reset_query();
Let me know if you need any more assistance. Thanks
just go to your wp-admin wordpress admin section and login, then select menu from sidebar as below
woocommerce -> settings -> products (tab) -> Display Option
check have 'Product images' title where you can set image size. so when u add/upload any new product image woocommerce auto crop image at your specify heitht and width
// You can pass "thumbnail", "medium", "large" and "full"
$cat_thumb_url = wp_get_attachment_image_src( $attachment->ID, 'full' );
echo $cat_thumb_url[0];

Get Products within Current Product Category

I'm currently playing around with WOOCOMMERCE V2.0.13 and I'm trying to display each product from the current product category (e.g. Construction Products when on the Construction Page), I've managed to display the single products from within the current category but if the product is also in another category (e.g Construction and Enviroment) then the current category breaks and shows zero products either from Construction or Enviroment.
If I could get some advice/help on displaying products from the current category and allow it to work with products that are in multiple categories I'd trully apreciate the help and time.
I'm more than happy to recode this entire section to make it work, here is my code below please let me know if I've missed anything.
Thank you.
<ul class="products">
<?php
global $post, $product;
$categ = $product - > get_categories();
$categ2 = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $categ);
?>
<?php
global $product;
$args = array('post_type' = > 'product', 'posts_per_page' = > '999', 'product_cat' = > $categ2, );
$loop = new WP_Query($args);
while ($loop - > have_posts()): $loop - > the_post();
global $product;
?>
<li>
<a href = "<?php echo get_permalink(); ?>">
<?php
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail($post - > ID, apply_filters('single_product_large_thumbnail_size', 'shop_single'));
$image_title = esc_attr(get_the_title(get_post_thumbnail_id()));
$image_link = get_permalink($product_id);
$attachment_count = count($product - > get_gallery_attachment_ids());
echo apply_filters('woocommerce_single_product_image_html', sprintf('%s', $image_link, $image_title, $image), $post - > ID);
} else {
echo apply_filters('woocommerce_single_product_image_html', sprintf('<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src()), $post - > ID);
} ?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php
if ($price_html = $product - > get_price_html()) {
?>
<span class = "price">
<?php echo $price_html; ?>
</span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt, 15);
?>
</p>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php if ( have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<div class="courses-main">
<ul class="products">
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = get_permalink( $product_id );
$attachment_count = count( $product->get_gallery_attachment_ids() );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php if ( $price_html = $product->get_price_html()) { ?>
<span class="price"><?php echo $price_html; ?></span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,15);
?>
</p>
</div>
</li>
<?php endwhile;?>
</ul>
</div>
<?php woocommerce_product_loop_end(); ?>
<?php endif; ?>

Custom post type loop

I need some guidance on this script.
I'm working on a custom post type loop but I'm stuck on how to convert this static html to a php loop
<?php
$loop = new WP_Query(array('post_type' => 'project', 'posts_per_page' => -1));
$count =0;
?>
<!--Text Sliders-->
<div class="ps-contentwrapper">
<?php if ( $loop ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$terms = get_the_terms( $post->ID, 'tagproject' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term )
{
$links[] = $term->name;
}
$links = str_replace(' ', '-', $links);
$tax = join( " ", $links );
else :
$tax = '';
endif;
?>
<?php $infos = get_post_custom_values('_url'); ?>
<div class="ps-content">
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div><!--end of ps-content-->
</div><!-- /ps-contentwrapper -->
<!--Image Sliders-->
<div class="ps-slidewrapper">
<div class="ps-slides">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div style="background-image: url(<?php echo $url; ?>);"></div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<nav>
</nav>
Here is the tutorial I'm trying to convert. Demo.
What I'm trying to figure out is how to line up everything dynamically. If someone can point me to the right direction I appreciate it.
My version of it with the current code I pasted above.
EDIT: Here is my code now after a bit of research. Now I'm trying to figure out how I can match the featured image to the appropriate post as it cycles through in this script. The div tag that echos the url needs to loop as many times the loop does and cycle appropriately.
<div class="ps-slides">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div style="background-image: url(<?php echo $url; ?>);"></div>
</div><!--end of ps-slides-->
Full code below:
<div class="ps-contentwrapper">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="ps-content">
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div><!--end of contentwrapper-->
<!--Image Sliders-->
<div class="ps-slidewrapper">
<div class="ps-slides">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div style="background-image: url(<?php echo $url; ?>);"></div>
</div><!--end of ps-slides-->
<nav>
<?php
$prev_post = get_previous_post();
$id = $prev_post->ID ;
$permalink = get_permalink( $id );
$prev_url = wp_get_attachment_url( get_post_thumbnail_id($id) );
?>
<?php
$next_post = get_next_post();
$nid = $next_post->ID ;
$permalink = get_permalink($nid);
$next_url = wp_get_attachment_url( get_post_thumbnail_id($nid) );
?>
</nav>
</div>
use code for your previous next post image url:
<?php
$prev_post = get_previous_post();
$id = $prev_post->ID ;
$permalink = get_permalink( $id );
$prev_url = wp_get_attachment_url( get_post_thumbnail_id($id) );
?>
<?php
$next_post = get_next_post();
$nid = $next_post->ID ;
$permalink = get_permalink($nid);
$next_url = wp_get_attachment_url( get_post_thumbnail_id($nid) );
?>

Categories