【发布时间】:2018-03-23 21:51:56
【问题描述】:
我需要将所有产品标签显示为主页上的下拉菜单。我尝试了以下代码,但没有成功。
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$term_array[] = $term->name;
}
}
它总是返回一个空数组。有什么建议么?谢谢。
【问题讨论】:
-
我尝试了该解决方案,但没有成功。
-
什么是“主页”?
-
像一个索引页,或主页。就我而言,我尝试在主题 function.php 文件中编写一个函数来获取 woo-commerce 产品的所有标签。我可以获取 ID、标题和内容,但不能获取标签。下面是我尝试过的另一段代码 sn-p 仍然没有运气:
-
$args = array('post_type' => 'product'); $list_tags = 数组(); $loop = new WP_Query($args); while ($loop->have_posts()) : $loop->the_post(); $terms = get_the_terms(get_the_ID(), 'product_tag');; if (!empty($terms) && !is_wp_error($terms)) { foreach ($terms as $term) { $list_tags = $term->slug; array_push($list_tags, $terms); } } 结束;返回 $list_tags;
标签: wordpress woocommerce