下拉选择器中的 WooCommerce 品牌分类术语列表
WooCommerce Brand taxonomy terms list in a dropdown selector
我不想使用任何插件来完成此任务,因为我最近遇到了与破坏站点的类似项目的冲突。所以我希望从基础开始创建这个功能。
我需要在产品类别页面上有一个下拉列表,以便按品牌 select 产品。下拉列表将显示所有品牌。当您 select 时,网站仅显示分配给该品牌的那些产品。我们不需要使用允许按新颖性、价格、受欢迎程度等查看的内置下拉列表
使用 WooCommerce 'Brands
' 分类法我已经设置了我的品牌并为每个产品分配了一个 品牌。
我可以使用以下代码查看所有品牌及其属性的数组:
$brands = get_terms('brand');
print_r($brands);
输出如下:
Array (
[0] => WP_Term Object ( [term_id] => 978 [name] => Imari Sometsuke [slug] => imari-sometsuke [term_group] => 0 [term_taxonomy_id] => 978 [taxonomy] => brand [description] => [parent] => 0 [count] => 1 [filter] => raw )
[1] => WP_Term Object ( [term_id] => 982 [name] => Kutani [slug] => kutani [term_group] => 0 [term_taxonomy_id] => 982 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[2] => WP_Term Object ( [term_id] => 977 [name] => Kutani Shoza [slug] => kutani-shoza [term_group] => 0 [term_taxonomy_id] => 977 [taxonomy] => brand [description] => [parent] => 0 [count] => 4 [filter] => raw )
[3] => WP_Term Object ( [term_id] => 979 [name] => Kutani Tokkuri [slug] => kutani-tokkuri [term_group] => 0 [term_taxonomy_id] => 979 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[5] => WP_Term Object ( [term_id] => 985 [name] => Nishikawa Sukenobu [slug] => nishikawa-sukenobu [term_group] => 0 [term_taxonomy_id] => 985 [taxonomy] => brand [description] => [parent] => 0 [count] => 1 [filter] => raw )
[6] => WP_Term Object ( [term_id] => 984 [name] => Shinsui Ito [slug] => shinsui-ito [term_group] => 0 [term_taxonomy_id] => 984 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[7] => WP_Term Object ( [term_id] => 976 [name] => Takeji Asano [slug] => takeji-asano [term_group] => 0 [term_taxonomy_id] => 976 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[8] => WP_Term Object ( [term_id] => 980 [name] => Toshusai Sharaku [slug] => toshusai-sharaku [term_group] => 0 [term_taxonomy_id] => 980 [taxonomy] => brand [description] => [parent] => 0 [count] => 3 [filter] => raw )
)
如何构建下拉列表 (select) 来创建此列表?
我想它会有类似我已经开始的框架:
<?php
$brands = get_terms('brand');
//print_r($brands);
?>
<select name="orderby" class="orderby">
<?php foreach ( $brands as ??? ) : ?>
<option value="<?php echo esc_attr( $??? ); ?>" <?php selected( $orderby, $??? ); ?>><?php echo esc_html( $??? ); ?></option>
<?php endforeach; ?>
</select>
如您所见,它是一个术语对象数组(WP_Term 对象),您必须在循环中为该术语的每个属性使用对象语法,这样:
<?php
$brands = get_terms( 'brand', array(
'orderby' => 'name' // orderby arguments ('name', 'slug','term_group', 'term_id', 'id', 'description')
) );
//print_r($brands);
?>
<select name="orderby" class="orderby">
<?php
foreach ( $brands as $key => $brand ) :
$brand_id = $brand->term_id;
$brand_name = $brand->name;
$brand_slug = $brand->slug;
$brand_term_group = $brand->term_group;
$brand_term_taxonomy = $brand->term_taxonomy_id;
$brand_taxonomy = $brand->taxonomy;
$brand_description = $brand->description;
$brand_parent = $brand->parent;
$brand_count = $brand->count;
$brand_filter = $brand->filter;
$number = $key+1;
$option = 'option-' . $number;
?>
<option value="<?php echo $option; ?>"><?php echo $brand->name; ?></option>
<?php endforeach; ?>
</select>
此代码已经过测试并且有效
In new version of WooCommerce the terms
has been prefix by pa_, so brand has been replaced by pa_brand
最新版本的工作代码为
<?php $brands = get_terms('pa_brand', ['orderby' => 'name']); ?>
<select name="orderby" class="orderby">
<?php
if (!empty($brands) && !is_wp_error($brands)):
foreach ($brands as $brand) :
?>
<option value="<?= $brand->term_id; ?>">
<?= $brand->name; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
参考:https://developer.wordpress.org/reference/functions/get_terms/
我不想使用任何插件来完成此任务,因为我最近遇到了与破坏站点的类似项目的冲突。所以我希望从基础开始创建这个功能。
我需要在产品类别页面上有一个下拉列表,以便按品牌 select 产品。下拉列表将显示所有品牌。当您 select 时,网站仅显示分配给该品牌的那些产品。我们不需要使用允许按新颖性、价格、受欢迎程度等查看的内置下拉列表
使用 WooCommerce 'Brands
' 分类法我已经设置了我的品牌并为每个产品分配了一个 品牌。
我可以使用以下代码查看所有品牌及其属性的数组:
$brands = get_terms('brand');
print_r($brands);
输出如下:
Array (
[0] => WP_Term Object ( [term_id] => 978 [name] => Imari Sometsuke [slug] => imari-sometsuke [term_group] => 0 [term_taxonomy_id] => 978 [taxonomy] => brand [description] => [parent] => 0 [count] => 1 [filter] => raw )
[1] => WP_Term Object ( [term_id] => 982 [name] => Kutani [slug] => kutani [term_group] => 0 [term_taxonomy_id] => 982 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[2] => WP_Term Object ( [term_id] => 977 [name] => Kutani Shoza [slug] => kutani-shoza [term_group] => 0 [term_taxonomy_id] => 977 [taxonomy] => brand [description] => [parent] => 0 [count] => 4 [filter] => raw )
[3] => WP_Term Object ( [term_id] => 979 [name] => Kutani Tokkuri [slug] => kutani-tokkuri [term_group] => 0 [term_taxonomy_id] => 979 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[5] => WP_Term Object ( [term_id] => 985 [name] => Nishikawa Sukenobu [slug] => nishikawa-sukenobu [term_group] => 0 [term_taxonomy_id] => 985 [taxonomy] => brand [description] => [parent] => 0 [count] => 1 [filter] => raw )
[6] => WP_Term Object ( [term_id] => 984 [name] => Shinsui Ito [slug] => shinsui-ito [term_group] => 0 [term_taxonomy_id] => 984 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[7] => WP_Term Object ( [term_id] => 976 [name] => Takeji Asano [slug] => takeji-asano [term_group] => 0 [term_taxonomy_id] => 976 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[8] => WP_Term Object ( [term_id] => 980 [name] => Toshusai Sharaku [slug] => toshusai-sharaku [term_group] => 0 [term_taxonomy_id] => 980 [taxonomy] => brand [description] => [parent] => 0 [count] => 3 [filter] => raw )
)
如何构建下拉列表 (select) 来创建此列表? 我想它会有类似我已经开始的框架:
<?php
$brands = get_terms('brand');
//print_r($brands);
?>
<select name="orderby" class="orderby">
<?php foreach ( $brands as ??? ) : ?>
<option value="<?php echo esc_attr( $??? ); ?>" <?php selected( $orderby, $??? ); ?>><?php echo esc_html( $??? ); ?></option>
<?php endforeach; ?>
</select>
如您所见,它是一个术语对象数组(WP_Term 对象),您必须在循环中为该术语的每个属性使用对象语法,这样:
<?php
$brands = get_terms( 'brand', array(
'orderby' => 'name' // orderby arguments ('name', 'slug','term_group', 'term_id', 'id', 'description')
) );
//print_r($brands);
?>
<select name="orderby" class="orderby">
<?php
foreach ( $brands as $key => $brand ) :
$brand_id = $brand->term_id;
$brand_name = $brand->name;
$brand_slug = $brand->slug;
$brand_term_group = $brand->term_group;
$brand_term_taxonomy = $brand->term_taxonomy_id;
$brand_taxonomy = $brand->taxonomy;
$brand_description = $brand->description;
$brand_parent = $brand->parent;
$brand_count = $brand->count;
$brand_filter = $brand->filter;
$number = $key+1;
$option = 'option-' . $number;
?>
<option value="<?php echo $option; ?>"><?php echo $brand->name; ?></option>
<?php endforeach; ?>
</select>
此代码已经过测试并且有效
In new version of WooCommerce the
terms
has been prefix by pa_, so brand has been replaced by pa_brand
最新版本的工作代码为
<?php $brands = get_terms('pa_brand', ['orderby' => 'name']); ?>
<select name="orderby" class="orderby">
<?php
if (!empty($brands) && !is_wp_error($brands)):
foreach ($brands as $brand) :
?>
<option value="<?= $brand->term_id; ?>">
<?= $brand->name; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
参考:https://developer.wordpress.org/reference/functions/get_terms/