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

问题描述 投票:1回答:1

我想按产品类别列出产品标签。它列出了标签但由于某些原因它们不是链接。

我已将代码添加到自定义小部件中。现在我的类别是硬编码的。我打算在未来改变这种状况。我想将类别添加为窗口小部件中的字段。

我的子function.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;
}   
}   
?>
php wordpress woocommerce
1个回答
2
投票

您没有锚标记来像链接一样工作。您的链接是选项标记的值。

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

在你的foreach循环中:

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

不要担心性能速度。它没有任何区别。而不是用破折号替换空格和小写字符串使slug WordPress具有内置方法的get_tag_link()

你的结束div是无效的。

© www.soinside.com 2019 - 2024. All rights reserved.