<div class="row">
<?php
$taxonomyName = "product_tag";
if($taxonomyname = "latest-product"){
//This gets top layer terms only. This is done by setting parent to 0.
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'term_id', 'hide_empty' => true, 'order' => 'ASC'));
foreach ($parent_terms as $pterm) { ?>
<div class="col-xs-3 col-md-3 padding_fix">
<a href="<?php echo get_term_link($pterm->name, $taxonomyName); ?>">
<?php
$thumbnail_id = get_woocommerce_term_meta($pterm->term_id, 'thumbnail_id', true);
// get the image URL for parent category
$image = wp_get_attachment_url($thumbnail_id);
echo '<img src="'.$image.'" alt="" width="762" height="365" />'; ?>
<h3 class="text-center" style="color: #fff;"><?php echo $pterm->name; ?></h3>
</a>
</div>
<?php } ?>
<?php } ?>
this is my above code where i want to display product with latest-product tag name.
I am not able to display product by latest-product tag.
<div class="row">
<?php
$taxonomyName = "product_tag";
if($taxonomyname = "latest-product"){
//This gets top layer terms only. This is done by setting parent to 0.
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'term_id', 'hide_empty' => true, 'order' => 'ASC'));
foreach ($parent_terms as $pterm) { ?>
<div class="col-xs-3 col-md-3 padding_fix">
<a href="<?php echo get_term_link($pterm->name, $taxonomyName); ?>">
<?php
$thumbnail_id = get_woocommerce_term_meta($pterm->term_id, 'thumbnail_id', true);
// get the image URL for parent category
$image = wp_get_attachment_url($thumbnail_id);
echo '<img src="'.$image.'" alt="" width="762" height="365" />'; ?>
<h3 class="text-center" style="color: #fff;"><?php echo $pterm->name; ?></h3>
</a>
</div>
<?php } ?>
<?php } ?>
this is my above code where i want to display product with latest-product tag name.
I am not able to display product by latest-product tag.
Finally Solved my problem My previous code was only for category wise product but when using tag it should be post. for better explain someone can please explain about the whole process.
<?php
$args = array(
'number' => $number,
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_tags = get_terms( 'product_tag', $args );
$count = count($product_tags);
if ( $count > 0 ){
foreach ( $product_tags as $product_tag ) {
// echo '<h4><a href="' . get_term_link( $product_tag ) . '">' . $product_tag->name . '</a></h4>';
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
// 'terms' => 'white-wines'
'terms' => $product_tag->slug,
'product_tag'=> "latest-product"
)
),
'post_type' => 'product',
'orderby' => 'title,'
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
<?php echo get_the_post_thumbnail( $post_id, 'thumbnail' ); ?>
</a>
</li>
<?php
}
echo "</ul>";
}
}
?>
product_tag is non-hierarchical product category. To fetch the products you will need to run the query as below :
add_action("admin_init","display_products");
function display_products()
{
ob_start();
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'product_tag' => array('latest-product')
);
$loop = new WP_Query( $args );
$product_count = $loop->post_count;
if( $product_count > 0 ){
echo '<ul class="products">';
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
global $post;
echo "<li>" . $loop->post->ID. " </li>";
endwhile;
echo '</ul>';
}else{
echo 'No product matching your criteria.';
}
$result = ob_get_clean();
echo $result;
}
Note : Just to check the snippet I have added it on admin_init hook You can change it.
Tax Query can be added in arguments for supporting multiple tags.
you need to query the products that are associated with that tag and display them accordingly. first you retrieve the term object for the latest-product
tag using get_term_by
then check if the tag exists $tag
and then use wc_get_products
to get the IDs of products associated with that tag.
<div class="row">
<?php
$taxonomyName = "product_tag";
$tag_slug = "latest-product"; // Specify the slug of the tag you want to display products for
// Get the term object for the specified tag
$tag = get_term_by('slug', $tag_slug, $taxonomyName);
// Make sure the tag exists and is valid
if ($tag) {
// Get the product IDs associated with the tag
$product_ids = wc_get_products(array(
'status' => 'publish',
'tag' => $tag->name,
'return' => 'ids',
));
// Loop through the product IDs
foreach ($product_ids as $product_id) {
$product = wc_get_product($product_id); ?>
<div class="col-xs-3 col-md-3 padding_fix">
<a href="<?php echo get_permalink($product_id); ?>">
<?php echo $product->get_image(); ?>
<h3 class="text-center" style="color: #fff;"><?php echo $product->get_name(); ?></h3>
</a>
</div>
<?php }
} ?>
</div>