Wordpress 存档页面 - 获取相关分类法
Wordpress Archive Page - Get related taxonomies
我目前正在为我的存档页面构建过滤器。由于我正在从事电子商务项目 (woocommerce),因此我使用了一些额外的自定义术语,例如品牌。考虑到用户在产品类别存档 T 恤上,我现在想在存档页面中显示与 T 恤相关的所有品牌。
使用 get_terms
我只能显示所有品牌,而不是 "cross-filtered" T 恤。非常感谢任何帮助。我的方法也可能完全错误。
到目前为止我的方法:(这不是常用方法)
$product_ids = get_posts(array(
'numberposts' => -1, // get all posts.
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term_id,
),
),
'fields' => 'ids', // Only get post IDs
));
foreach ($product_ids as $test_id) {
$aaa = wp_get_post_terms($test_id, 'manufacturer', array('fields' => 'ids'));
$brands[] = $aaa[0];
}
$brands = array_unique($brands);
我无法弄清楚 "brands" 是什么。您的 wp_get_post_terms 语句将获取分类法中每个类别的所有帖子 "manufacturer." 如果制造商 = 品牌,您可以添加此循环以循环遍历它们:
foreach ($product_ids as $test_id) {
$post = get_post($test_id);
echo '>> ' . $post->post_name . '</br>';
$aaa = wp_get_post_terms($test_id, 'manufacturer',
array('fields' => 'id=>slug'));
foreach( $aaa as $a) {
echo $a . '</br>';
}
}
然后您可以添加 'if' 语句来进行比较。
我目前正在为我的存档页面构建过滤器。由于我正在从事电子商务项目 (woocommerce),因此我使用了一些额外的自定义术语,例如品牌。考虑到用户在产品类别存档 T 恤上,我现在想在存档页面中显示与 T 恤相关的所有品牌。
使用 get_terms
我只能显示所有品牌,而不是 "cross-filtered" T 恤。非常感谢任何帮助。我的方法也可能完全错误。
到目前为止我的方法:(这不是常用方法)
$product_ids = get_posts(array(
'numberposts' => -1, // get all posts.
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term_id,
),
),
'fields' => 'ids', // Only get post IDs
));
foreach ($product_ids as $test_id) {
$aaa = wp_get_post_terms($test_id, 'manufacturer', array('fields' => 'ids'));
$brands[] = $aaa[0];
}
$brands = array_unique($brands);
我无法弄清楚 "brands" 是什么。您的 wp_get_post_terms 语句将获取分类法中每个类别的所有帖子 "manufacturer." 如果制造商 = 品牌,您可以添加此循环以循环遍历它们:
foreach ($product_ids as $test_id) {
$post = get_post($test_id);
echo '>> ' . $post->post_name . '</br>';
$aaa = wp_get_post_terms($test_id, 'manufacturer',
array('fields' => 'id=>slug'));
foreach( $aaa as $a) {
echo $a . '</br>';
}
}
然后您可以添加 'if' 语句来进行比较。