【问题标题】:Wordpress Archive Page - Get related taxonomiesWordpress 存档页面 - 获取相关分类法
【发布时间】:2020-02-11 18:34:30
【问题描述】:

我目前正在为我的存档页面构建过滤器。由于我正在从事电子商务项目(woocommerce),因此我使用了一些额外的自定义术语,例如品牌。考虑到用户在产品类别存档 T 恤上,我现在想在存档页面中显示与 T 恤相关的所有品牌。

使用get_terms,我只能显示所有品牌,但不能被 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);

【问题讨论】:

    标签: wordpress taxonomy-terms


    【解决方案1】:

    我很难弄清楚什么是“品牌”。您的 wp_get_post_terms 语句将获取分类“制造商”中每个类别的所有帖子。如果制造商 = 品牌,您可以添加此循环以循环它们:

    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”语句进行比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多