Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter webvn

    (@webvn)

    help me please !

    Thread Starter webvn

    (@webvn)

    <?php
    $term = get_queried_object();
    $product_category = $term->slug;
    
    //Iterate through all products in this category
    $query_args = array(
    
    'product_cat' => $product_category,
    'post_type' => 'product',
    
    //Grabs ALL post
    'posts_per_page' => -1
    );
    
    $query = new WP_Query( $query_args );
    $term_array = array();
    
    while( $query->have_posts() ) {
    $query->the_post();
    $terms = get_the_terms( get_the_ID(), 'product_tag' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
    $term_array[] = $term->name;
    }
    }
    }
    
    //Remove any duplicates.
    $tags_unique = array_unique($term_array);
    
    //Sort alphabetically
    asort($tags_unique);
    
    //Output
    echo ''; echo 'Filter by Tag'; foreach($tags_unique as $unique) { //it's faster to "guess" the tag slug by replacing spaces with dashes and stripping special chars $special_characters = array("=", "+", "/", "'",")","("); $tag_slug = str_replace(" ","-",$unique); $tag_slug = strtolower(str_replace($special_characters,"",$tag_slug)); echo ' '. $unique . ''; } echo '';
    
    //Reset the query
    wp_reset_postdata();
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)