admin管理员组

文章数量:1289541

so I have put together a function that filters products by a specific tag and displays their categories.

What i want to do, is extend it further so that when the customer clicks the category link ( that i have in the function ), and have it only show products in that category ( on the product page for that category )

is it possible for a URL to filter by tag? can i amend the url link so that it takes the tag ( I am yet to make this into a dynamic variable for the shortcode ). it's called testingtag below


function display_products()
{
 ob_start();

 $args = array( 
            'post_type'      => 'product', 
            'posts_per_page' => -1, 
            'product_tag'    => array('testingtag'),
            );
 $loop = new WP_Query( $args );
 $product_count = $loop->post_count;

 $return_string ='';
 $start_string ='';

 if( $product_count > 0 ){
    $start_string = '<div class="woocommerce columns-4"><ul class="products columns-4">';
    while ( $loop->have_posts() ) : $loop->the_post(); 
            global $product;
            global $post;
            $loop->post->ID;             
            $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );   
                     
            foreach( $product_cats as $collection ) {            
            $test = $collection->slug;
                if ( $product_cats && ! is_wp_error ( $product_cats ) ){
                    $single_cat = array_shift( $product_cats );
                    $return_string = '<li class="product-category product brand_cats"><div class="banner-box"><div class="banner-box__image"> <a href="' . get_bloginfo( 'url' ) . '/'. 'product-category'. '/' . $test . '/' . '"';
                    glana_woo_subcategory_thumbnail( $collection);                
                    $return_string .=  '</a></div>';
                    $return_string .=  '<div class="banner-box__info"><a href="' . get_bloginfo( 'url' ) . '/'. 'product-category'. '/' . $test . '/' . '">' . '<h2 itemprop="name" class="woocommerce-loop-category__title"><span>' . $single_cat->name . '</a></span></h2></li>'; }  
            }
    endwhile;
    $return_string .= '</ul></div>';
 }else{
       echo 'No product matching your criteria.';
 }
  $start_string .= $return_string;

  return $start_string;
 ob_get_clean();

 }
 add_shortcode('display_products_by_tag', 'display_products');

本文标签: phpis it possible for a URL to filter by tag