在 Woocommerce 的任何页面上仅显示单个链接的产品类别文本
Display only a single linked product category text on any page in Woocommerce
我需要在几个常规页面上显示不同的产品类别,但找不到合适的短代码。
我必须使用哪个短代码才能在任何页面上仅显示单个产品类别(不显示该类别的任何产品)?
感谢任何帮助。
我认为您的要求是从产品类别中获取 link 以显示 linked 产品类别名称。为此,您可以构建自定义简码:
add_shortcode('linked_pcat', 'display_a_linked_product_category');
function display_a_linked_product_category( $atts ) {
$atts = shortcode_atts( array(
'term' => '',
), $atts, 'linked_pcat' );
$taxomomy = 'product_cat';
$value = $atts['term'];
if( is_numeric( $value ) || (string) (int) $value == $value ) {
$field = 'term_id';
$value = (int) $atts['term'];
} else {
$field = 'slug';
$value = sanitize_title( $atts['term'] );
}
if( term_exists( $value, $taxomomy ) ) {
$term = get_term_by( $field, $value, $taxomomy );
$term_link = get_term_link( $term, $taxomomy );
return '<a href="' . $term_link . '">' . $term->name . '</a>';
}
return false;
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。
用法:
Note: For the product category term can use a term name, a term slug or a term ID…
1) 在 Wordpress 文本编辑器中 (以术语名称为例):
[linked_pcat term='Clothing']
2) php 内部代码 (例如,带有术语 slug):
echo do_shortcode( "[linked_pcat term='t-shirts']" );
3) 在 php 文件中 html 代码 (例如术语 ID):
<?php echo do_shortcode( "[linked_pcat term='t-shirts']" ); ?>
我需要在几个常规页面上显示不同的产品类别,但找不到合适的短代码。
我必须使用哪个短代码才能在任何页面上仅显示单个产品类别(不显示该类别的任何产品)?
感谢任何帮助。
我认为您的要求是从产品类别中获取 link 以显示 linked 产品类别名称。为此,您可以构建自定义简码:
add_shortcode('linked_pcat', 'display_a_linked_product_category');
function display_a_linked_product_category( $atts ) {
$atts = shortcode_atts( array(
'term' => '',
), $atts, 'linked_pcat' );
$taxomomy = 'product_cat';
$value = $atts['term'];
if( is_numeric( $value ) || (string) (int) $value == $value ) {
$field = 'term_id';
$value = (int) $atts['term'];
} else {
$field = 'slug';
$value = sanitize_title( $atts['term'] );
}
if( term_exists( $value, $taxomomy ) ) {
$term = get_term_by( $field, $value, $taxomomy );
$term_link = get_term_link( $term, $taxomomy );
return '<a href="' . $term_link . '">' . $term->name . '</a>';
}
return false;
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。
用法:
Note: For the product category term can use a term name, a term slug or a term ID…
1) 在 Wordpress 文本编辑器中 (以术语名称为例):
[linked_pcat term='Clothing']
2) php 内部代码 (例如,带有术语 slug):
echo do_shortcode( "[linked_pcat term='t-shirts']" );
3) 在 php 文件中 html 代码 (例如术语 ID):
<?php echo do_shortcode( "[linked_pcat term='t-shirts']" ); ?>