根据产品Woocommerce / PHP的标签显示产品类别

时间:2017-06-15 23:52:12

标签: php mysql wordpress woocommerce

目前我正在使用此类别显示代码

<?php
    $args = array(
    'number' => 100,
    'orderby' => 'title',
    'order' => 'ASC',
    'hide_empty' => false
);
$product_categories = get_terms('product_cat', $args);
$count = count($product_categories);
if ($count > 0) {
foreach($product_categories as $product_category) {
$cat_thumb_id = get_woocommerce_term_meta($product_category - > term_id,'thumbnail_id', true);
$cat_thumb_url = wp_get_attachment_thumb_url($cat_thumb_id);
$term_link = get_term_link($product_category, 'product_cat');
echo '<a href="'.$term_link.'"><img src="'.$cat_thumb_url.'"/></a>';
echo '<h4><a href="'.get_term_link($product_category).'">'.$product_category - > name.'</a></h4>';
}
}
?>

但我希望改进此代码以显示基于产品标签的产品类别有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

玩完我的代码后,我得到了我想要显示的内容。 BTW

  

$ STR

代表您要显示的产品标签

<?php                                             
  $args = array(
    'number'     => 100,
      'orderby'    => 'title',
        'order'      => 'ASC',
        'hide_empty' => false); 
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
$args = array(
     'posts_per_page' => -1,
     'tax_query' => array(
         'relation' => 'AND',
         array(
             'taxonomy' => 'product_cat',
             'field' => 'slug',
             'terms' => 'white-wines'
'terms' => $product_category - > slug
),
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
// 'terms' => 'white-wines'
'terms' => array($str)
)
),
'post_type' => 'product',
'orderby' => 'title,'
);
$products = new WP_Query($args);
echo "<ul>";
while ($products - > have_posts()) {
    $products - > the_post();

    $cat_thumb_id = get_woocommerce_term_meta($product_category - > term_id, 'thumbnail_id', true);
    $cat_thumb_url = wp_get_attachment_thumb_url($cat_thumb_id);
    $term_link = get_term_link($product_category, 'product_cat');

    echo '<a href="'.$term_link.
    '"><img src="'.$cat_thumb_url.
    '"/></a>';
    echo '<h4><a class="'.$product_category - > slug.
    '" href="'.get_term_link($product_category).
    '">'.$product_category - > name.
    '</a></h4>';
    /*  echo '<h1><a class="' . $product_category->slug . '" href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h1>';*/
    ? >
    <!-- <li>
    < a href = "<?php the_permalink(); ?>" >
        <?php the_title(); ?> < /a> < /li>-->
    <?php
                                                            }
                                                            echo "</ul>";
                                                        }
                                                    }
                                                ?>

如果你们有更好的想法,请在这里发布,以便我们可以互相帮助谢谢。

相关问题