Karen
Karen

Reputation: 11

Product tag list by product category - tags are not clickable or links

I am trying to list product tags by product category. It is listing the tags but they are not links, for some reason.

I have added the code to a custom widget. Right now I have the category hard coded. I intend to change that down the road. I would like to add the category as a field in the widget.

I have the following code in my child functions.php:

<?php 
function CustomProductTags_widget() {
register_widget( 'CustomProductTags_widget' );
}

add_action( 'widgets_init', 'CustomProductTags_widget' );

class CustomProductTags_widget extends WP_Widget {

public function __construct() {
parent::__construct(
// widget ID
'CustomProductTags_widget',
// widget name
__('Custom Product Tags', ' CustomProductTags_widget_domain'),
// widget description
array( 'description' => __( 'CustomProductTags Widget', 'CustomProductTags_widget_domain' ), )
);
}
public function widget() {
add_action( 'widgets_init', 'CustomProductTags_widget' );
//Get the current category (could also put the desired slug or id into $product_category directly)
$term = get_queried_object();
$product_category = commercial;

//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 '<select onchange="window.location=this.options[this.selectedIndex].value">';

//echo '<option value="Filter by Tag">Filter by Tag</option>';
echo '<div class="tagcloud">';
                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 '<option value="https://www.karenyetter/category/'. $product_category .'/?product_tag='. $tag_slug .'">'. $unique . '</option>';
        }
//echo '</select>'; 
echo '</div';


//Reset the query
wp_reset_postdata();
}


public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) )
$title = $instance[ 'title' ];
else
$title = __( 'Default Title', 'hstngr_widget_domain' );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}

public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}   
}   
?>

Upvotes: 1

Views: 399

Answers (1)

Ashish Yadav
Ashish Yadav

Reputation: 2008

You don't have anchor tag to work it like a link. Your link is a value of option tag.

You can use get_tag_link() function to get the link.

In your foreach loop:

foreach($tags_unique as $unique) {
    echo get_tag_link($unique->term_id); 
}

Don't worry about performance speed. It doesn't makes any difference. Rather than replacing spaces with dash and lowercasing the string to make slug WordPress has get_tag_link() built in method.

Your closing div is invalid.

Upvotes: 3

Related Questions