Woocommerce - 在循环中显示类别
Woocommerce - Display categories within the loop
是否可以在 WooCommerce 循环中在磁贴下方显示 产品类别?
这是我在 mytheme/woocommerce/content-product.php
中包含的代码,摘自 Wordpress documentation on get_the_category()
;但它似乎根本没有输出任何东西
<a href="<?php the_permalink(); ?>" class="titulo-insumo" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
<div class="label-group"><!--Categories *should* be outputed here -->
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= '<a href="'.get_category_link( $category->term_id ).'" class="label bg-terciary" title="' . esc_attr( sprintf( __( "Ver todos los artículos en la categoría %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
}
echo trim($output, $separator);
}
?>
</div>
好吧,没花多少时间就弄清楚了,所以我把我的结果贴在这里。
类别使用 the get_the_terms() wordpress function 输出,link 使用简单的获取请求 (/?product_cat=
) 输出,我认为这不适用于任何类型的漂亮 permalinks启用选项
<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) : //only displayed if the product has at least one category
$cat_links = array();
foreach ( $terms as $term ) {
$cat_links[] = '<a class="label bg-terciary" href="'.get_site_url().'/?product_cat='.$term->slug.'" title="'.$term->name.'">'.$term->name.'</a>';
}
$on_cat = join( " ", $cat_links );
?>
<div class="label-group">
<?php echo $on_cat; ?>
</div>
<?php endif; ?>
是否可以在 WooCommerce 循环中在磁贴下方显示 产品类别?
这是我在 mytheme/woocommerce/content-product.php
中包含的代码,摘自 Wordpress documentation on get_the_category()
;但它似乎根本没有输出任何东西
<a href="<?php the_permalink(); ?>" class="titulo-insumo" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
<div class="label-group"><!--Categories *should* be outputed here -->
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= '<a href="'.get_category_link( $category->term_id ).'" class="label bg-terciary" title="' . esc_attr( sprintf( __( "Ver todos los artículos en la categoría %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
}
echo trim($output, $separator);
}
?>
</div>
好吧,没花多少时间就弄清楚了,所以我把我的结果贴在这里。
类别使用 the get_the_terms() wordpress function 输出,link 使用简单的获取请求 (/?product_cat=
) 输出,我认为这不适用于任何类型的漂亮 permalinks启用选项
<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) : //only displayed if the product has at least one category
$cat_links = array();
foreach ( $terms as $term ) {
$cat_links[] = '<a class="label bg-terciary" href="'.get_site_url().'/?product_cat='.$term->slug.'" title="'.$term->name.'">'.$term->name.'</a>';
}
$on_cat = join( " ", $cat_links );
?>
<div class="label-group">
<?php echo $on_cat; ?>
</div>
<?php endif; ?>