gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:46:41 25 4
gpt4 key购买 nike

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

<?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>';
}
}
?>

但我想改进此代码以根据产品标签显示产品类别,有办法做到这一点吗?

最佳答案

使用我的代码后,我得到了我想要显示的内容。顺便说一句

$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>";
}
}
?>

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

关于php - 根据产品标签显示产品类别 Woocommerce/PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44578723/

25 4 0