具有特定字符串的 Woocommerce 标签列表
Woocommerce tag list having specific string
该代码非常适合列出 WooCommerce 产品标签,但我想用它添加一个查询。
我只想列出包含特定字符串的产品标签。
<?php
$terms = get_terms(array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
));
$count = count($terms);
echo "found ". $count . " Schools";
?>
<div class="product-tags">
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><a href="<?php echo get_term_link( $term->term_id, 'product_tag' ); ?> " rel="tag"><?php echo $term->name; ?></a></li>
<?php } ?>
</ul>
</div>
根据此处的 wordpress 文档,您可以在第一个参数数组中使用 'search' 或 'name_like' 字段:
https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
例如,假设您想获取名称包含 'foo'
的所有术语
<?php
$terms = get_terms(array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
'name__like' => '%foo%'
));
使用WP_Term_Query
代替get_terms
$keyword = 'tag';
// Args
$args = array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
'name__like' => $keyword,
);
// Term Query
$query = new WP_Term_Query($args);
// Get terms
$get_terms = $query->get_terms();
// Count
$count = count( $get_terms );
echo "found ". $count . " Schools";
// Loop
foreach ( $get_terms as $terms ) {
echo $terms->name;
}
该代码非常适合列出 WooCommerce 产品标签,但我想用它添加一个查询。
我只想列出包含特定字符串的产品标签。
<?php
$terms = get_terms(array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
));
$count = count($terms);
echo "found ". $count . " Schools";
?>
<div class="product-tags">
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><a href="<?php echo get_term_link( $term->term_id, 'product_tag' ); ?> " rel="tag"><?php echo $term->name; ?></a></li>
<?php } ?>
</ul>
</div>
根据此处的 wordpress 文档,您可以在第一个参数数组中使用 'search' 或 'name_like' 字段:
https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
例如,假设您想获取名称包含 'foo'
的所有术语<?php
$terms = get_terms(array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
'name__like' => '%foo%'
));
使用WP_Term_Query
代替get_terms
$keyword = 'tag';
// Args
$args = array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
'name__like' => $keyword,
);
// Term Query
$query = new WP_Term_Query($args);
// Get terms
$get_terms = $query->get_terms();
// Count
$count = count( $get_terms );
echo "found ". $count . " Schools";
// Loop
foreach ( $get_terms as $terms ) {
echo $terms->name;
}