【发布时间】:2015-10-07 08:08:03
【问题描述】:
是否可以获得 WooCommerce 产品标签列表,但前提是它们与一系列特定产品相关联?
我有一系列产品 ID,但不知道从哪里开始 - 我查看了 Wordpress Codex 文档中的许多 get_tag 函数,但它们似乎都是针对单个帖子/页面/自定义帖子仅用于类型,不适用于数组。
【问题讨论】:
标签: arrays wordpress tags woocommerce woothemes
是否可以获得 WooCommerce 产品标签列表,但前提是它们与一系列特定产品相关联?
我有一系列产品 ID,但不知道从哪里开始 - 我查看了 Wordpress Codex 文档中的许多 get_tag 函数,但它们似乎都是针对单个帖子/页面/自定义帖子仅用于类型,不适用于数组。
【问题讨论】:
标签: arrays wordpress tags woocommerce woothemes
在 WisdmLabs 的帮助下,我设法将一些代码放在一起,以便为特定 WooCommerce 品牌中的所有产品找到产品标签。
我还添加了一些功能,例如删除重复的标签值,并在每个标签旁边添加带有该标签的产品数量的总数,因此输出看起来像......
• Product Tag 1 (12)
• Product Tag 3 (4)
• Product Tag 4 (7)
• Product Tag 8 (11)
下面的代码供任何想要使用或改编它的人使用。我正在使用 WooCommerce 品牌挂钩 woocommerce_archive_description 在品牌描述下方输出结果。
function get_wc_product_brand_related_tags() {
global $post;
// Access the objects in the current WooCommerce query loop
$queried_object = get_queried_object();
// Get the term_id and term_name values
$term_id = $queried_object->term_id;
$term_name = $queried_object->slug;
// FOR TESTING ONLY - Echo the term_id and term_name values to see if they are what we're expecting
//echo '<p>The <em>term_id</em> is <strong>' . $term_id . '</strong> and the <em>term_name</em> is <strong>' . $term_name . '</strong>.</p>';
// Create our products array for use later
$products_array = array();
// Query the posts using the product custom post type and product_brand custom taxonomy terms
$brand_post_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'product_brand' => $term_name
);
// Run our WP_Query with the args above
$brand_post_query = new WP_Query($brand_post_args);
// If any products (posts using the product custom post type) are found…
if( $brand_post_query->have_posts() ) {
while ($brand_post_query->have_posts()) : $brand_post_query->the_post();
// FOR TESTING ONLY - shows a lot more information including product id, name and tags
$products_array[] = get_the_id() . ' - ' . get_the_title() . ' - ' . get_the_term_list($brand_post_query->ID, 'product_tag');
// Populate the array with the tags found for the product
$products_tags_array[] = get_the_term_list($brand_post_query->ID, 'product_tag');
endwhile;
}
// Count the duplicate values found inside our array
$count_tag_totals = array_count_values($products_tags_array);
// FOR TESTING ONLY - Used below to show the product information such as id, name etc
$show_product_values = array_unique($products_tags_array);
// Show the tags found for all of the products for the current brand, along with the number found for each tag
echo '<ul>';
foreach($count_tag_totals as $key=>$value) {
echo '<li>' . $key . ' ('. $value . ')</li>';
}
echo '</ul>';
// FOR TESTING ONLY - Displays unique tag name(s) along with a total of the products that have that tag
//echo '<pre><h3>Unique product tag(s) and associated totals for this brand… </h3>',print_r($count_tag_totals,1),'</pre>';
// FOR TESTING ONLY - Displays a list of all products (id and name) along with their tags
//echo '<pre><h3>The products details for this brand page are… </h3>',print_r($products_array,1),'</pre>';
// Reset the WP_Query for use elsewhere
wp_reset_query();
}
add_action('woocommerce_archive_description', 'get_wc_product_brand_related_tags', 15);
【讨论】:
为什么不遍历数组获取产品列表的所有标签。
这是您可以使用的代码:
function wdm_get_tags($products_array){
echo "Tags: ";
foreach($products_array as $single_product)
{
echo get_the_term_list($single_product, 'product_tag', '', ',' );
echo ',';
}
}
下面是测试函数的代码:
$products=array(378,356);
wdm_get_tags($products);
这是我尝试过的演示设置的输出:
此代码运行良好。让我知道这是否解决了您的问题。
【讨论】: