As an eCommerce business owner, you’re always looking for new ways to promote your products and reach a wider audience. One effective strategy is to display your WooCommerce products on related blog posts. By doing so, you can capture the attention of readers who are already interested in your niche and increase your chances of making a sale.
In this article, we’ll show you how to display WooCommerce products by tag to related blogs in WordPress. We’ll cover everything you need to know, from creating product tags to integrating them with your blog posts. So, let’s get started!
Before we can display our products by tag, we need to create those tags and assign to related products and blogs.
and insert these code.
/* css */
.suggested_prod ul{
display: grid;
grid-auto-columns: 1fr;
grid-auto-flow: row;
grid-template-columns: repeat(auto-fill,minmax(250px,1fr));
}
.suggested_prod li {
list-style: none;
}
.suggested_prod li a p {
color: #000000a8;
text-align: center;
font-weight: 600;
}
.suggested_prod li a {
text-decoration: none;
}
if( has_tag($tag_slug_or_id, $post_id_or_object) ) {
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$tag_name[]= $tag->name . ' ';
}
}
?><h2><b>Suggested Products<
/b></h2> $number,
'orderby' => 'date',
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_tags = get_terms( 'product_tag', $args );
$count = count($product_tags);
if ( $count > 0 ){
?><div class="suggested_prod"><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' => $tag_name,
'product_tag'=> "latest-product"
)
),
'post_type' => 'product',
'orderby' => 'date,'
);
$products = new WP_Query( $args );
echo "<ul class='sugg_products'>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="">
<p></p>
get_regular_price();?>
</a>
</li>
</ul>";
}
?></div>