Php 如何检查woocommerce产品是否分配了任何类别?
我正在创建一个自定义插件,根据分配给产品的标签获取相关产品。我面临一个问题。当我运行循环时,它会给我一条警告消息。我想检查的是,特定产品是否分配了任何类别。如何检查是否已将任何类别分配给woocommerce产品。请帮帮我。提前谢谢 我的自定义代码Php 如何检查woocommerce产品是否分配了任何类别?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在创建一个自定义插件,根据分配给产品的标签获取相关产品。我面临一个问题。当我运行循环时,它会给我一条警告消息。我想检查的是,特定产品是否分配了任何类别。如何检查是否已将任何类别分配给woocommerce产品。请帮帮我。提前谢谢 我的自定义代码 while ( $loop->have_posts() ) : $loop->the_post(); global $product; global $post; $feat_image = wp_get_at
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
global $post;
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($loop->post->ID) );
$terms200 = get_the_terms( $loop->post->ID, 'product_cat' );
// if(!empty($terms200) || $terms200!='' || $terms200!=null){
if (get_category($loop->post->ID)->category_count != 0){
$content.='<li class="product-small grid1 grid-normal">';
$content.='<div class="inner-wrap">';
$content.='<a href="'.get_the_permalink($loop->post->ID).'">';
$content.='<div class="product-image hover_fade_in_back">';
$content.='<div class="front-image">';
$content.='<img src="'.$feat_image.'" class="attachment-shop_catalog wp-post-image" style="width:247px !important;height:300px !important;" />';
$content.='</div>';
$content.='</div>';
$content.='</a>';
$content .='<div class="info style-grid1">';
$content .='<div class="text-center">';
$count=1;
foreach ( @$terms200 as $term ) {
if($count==1){
$content.='<h5 class="category"><a href="'.get_site_url().'/?product_cat='.$term->slug.'" rel="tag">'.$term->name.'</a></h5>';
$content.='<div class="tx-div small"></div>';
}
$count++;
}
$content .='<a href="'.get_the_permalink($loop->post->ID).'"><p class="name">'.$loop->post->post_title.'</p></a>';
if ( $price_html = $product->get_price_html() ) :
$content .='<span class="price"><span class="amount">'.$price_html.'</span></span>';
endif;
$content.='<div class="mycustomcartbuttonbox">[add_to_cart id="'.$loop->post->ID.'"]</div>';
$content .='</div>';
$content .='</div>';
$content.='</div>';
$content.='</li>';
echo do_shortcode($content);
}
endwhile;
while($loop->have_posts()):$loop->the_post();
全球$产品;
全球$员额;
$feat_image=wp_get_attachment_url(get_post_缩略图_id($loop->post->id));
$terms200=获取_术语($loop->post->ID,'product_cat');
//如果(!empty($terms200)| |$terms200!=''| |$terms200!=null){
如果(获取类别($loop->post->ID)->类别计数!=0){
$content.=';
$content.='';
$content.='';
$content.='';
$content.='';
$count=1;
foreach(@$terms200作为$term){
如果($count==1){
$content.='';
$content.='';
}
$count++;
}
$content.='';
如果($price\u html=$product->get\u price\u html()):
$content.=''.$price_html';
endif;
$content.='[add_to_cart id=“”。$loop->post->id.“]”;
$content.='';
$content.='';
$content.='';
$content.=' ';
echo do_短代码($content);
}
结束时;
这样做其实很简单
只需在循环内部键入:
if(!has_category("uncategorized")){
//do stuff
}
如果这不起作用,您可以尝试:
if(!has_term( 'uncategorized', 'product_cat' )){
//do stuff
}
你可以在以下网址了解更多信息:@NitinJohnson我相信它会的,如果它不起作用,我已经将我的帖子编辑成了另一个选项。@NitinJohnson-你需要使用has_术语来表示WooCommerce。has_术语('category_name'、'product_cat'、$product->post)有没有做到这一点