按产品类别列出的产品标签-标签不可点击或链接

时间:2018-11-06 21:43:04

标签: php wordpress woocommerce

我正在尝试按产品类别列出产品标签。它列出了标签,但由于某种原因它们不是链接。

我已将代码添加到自定义小部件中。现在,我已经对该类别进行了硬编码。我打算改变这一点。我想将类别添加为小部件中的字段。

我的子函数.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;
}   
}   
?>

1 个答案:

答案 0 :(得分:2)

您没有定位标记,无法像链接一样使用它。您的链接是一个选项标签的值。

您可以使用get_tag_link()函数来获取链接。

在您的foreach循环中:

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

不用担心性能速度。没什么区别。而不是用破折号代替空格并减小字符串的大小写以使首尾缩略WordPress具有get_tag_link()内置方法。

您的关闭div无效。