我有3个产品类别-类别1、类别2和CATEGOTRY-3
我还创建了3个额外的自定义分类法作为每个类别的标签,它们是TAGS-1、TAGS-2和TAGS-3。
我已经编辑了Woocommerce单一产品,并创建了一个自定义选项卡。在此选项卡中,我希望根据类别选择显示我的自定义标记。
因此,如果为产品选择了CATEGORY-1,那么我想显示标签-1,如果选择了CATEGORY-2,那么我想显示标签-2,最后一组标签也是如此。
我的代码目前看起来像这样:
<?php global $post, $product, $woocommerce;
if ( has_term( 'CATEGORY-1', 'product_cat' ) ) {
$tags = wp_get_object_terms( $post->ID, 'TAGS-1' );
} elseif ( has_term( 'CATEGORY-2', 'product_cat' ) ) {
$tags = wp_get_object_terms( $post->ID, 'TAGS-2' );
} elseif ( has_term( 'CATEGORY-3', 'product_cat' ) ) {
$tags = wp_get_object_terms( $post->ID, 'TAGS-3' );
}
return $tags;
if ( $tags ) : ?>
<ul id="TAGS-LIST">
<?php foreach ( $tags as $tag ) : ?>
<li>
<?php $tag_link = esc_url( get_term_link( $tag ) ); ?>
<a href="<?php echo $tag_link; ?>" class="TAG-CLASS">
<span class="TAG-TEXT-CLASS"><?php echo $tag->name; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul><!-- #TAG-LIST -->
<?php endif; ?>但不幸的是,我似乎不能让它工作。根本没有显示任何输出。我哪里错了?
发布于 2018-01-20 13:05:06
如果不知道产品数据,就很难说出确切的原因。我会在其中放一些echo语句,试图找出一些意想不到的事情发生在哪里。例如:
<?php global $post, $product, $woocommerce;
if ( has_term( 'CATEGORY-1', 'product_cat' ) ) {
$tags = wp_get_object_terms( $post->ID, 'TAGS-1' );
} elseif ( has_term( 'CATEGORY-2', 'product_cat' ) ) {
$tags = wp_get_object_terms( $post->ID, 'TAGS-2' );
} elseif ( has_term( 'CATEGORY-3', 'product_cat' ) ) {
$tags = wp_get_object_terms( $post->ID, 'TAGS-3' );
}
echo 'cat = ' . wp_get_object_terms( $post->ID, 'product_cat' );
echo 'tags1 = ' . wp_get_object_terms( $post->ID, 'TAGS-1' );
echo 'tags2 = ' . wp_get_object_terms( $post->ID, 'TAGS-2' );
echo 'tags3 = ' . wp_get_object_terms( $post->ID, 'TAGS-3' );
return $tags;
if ( $tags ) { ?>
<ul id="TAGS-LIST">
<?php foreach ( $tags as $tag ) : ?>
<li>
<?php $tag_link = esc_url( get_term_link( $tag ) ); ?>
<a href="<?php echo $tag_link; ?>" class="TAG-CLASS">
<span class="TAG-TEXT-CLASS"><?php echo $tag->name; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul><!-- #TAG-LIST -->
<?php } else { echo 'no tags present'; } ?>这有帮助吗?
发布于 2018-01-20 17:54:36
好吧,我只是走了很长一段路,但是虽然现在一切正常,但有没有办法让整个过程变得更短??
<div id="product-tags-wrapper">
<div class="products-tags-container">
<?php global $post, $product, $woocommerce;
if ( has_term( 'CATEGORY-1', 'product_cat' ) ) {
$tags = wp_get_object_terms( $post->ID, 'TAGS-1' );
if ( $tags ) : ?>
<ul id="TAGS-LIST">
<?php foreach ( $tags as $tag ) : ?>
<li>
<?php $tag_link = esc_url( get_term_link( $tag ) ); ?>
<a href="<?php echo $tag_link; ?>" class="TAG-CLASS">
<span class="TAG-TEXT-CLASS"><?php echo $tag->name; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul><!-- #TAG-LIST -->
<?php endif; ?>
<?php } elseif ( has_term( 'CATEGORY-2', 'product_cat' ) ) {
$tags = wp_get_object_terms( $post->ID, 'TAGS-2' );
if ( $tags ) : ?>
<ul id="TAGS-LIST">
<?php foreach ( $tags as $tag ) : ?>
<li>
<?php $tag_link = esc_url( get_term_link( $tag ) ); ?>
<a href="<?php echo $tag_link; ?>" class="TAG-CLASS">
<span class="TAG-TEXT-CLASS"><?php echo $tag->name; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul><!-- #TAG-LIST -->
<?php endif; ?>
<?php } elseif ( has_term( 'CATEGORY-3', 'product_cat' ) ) {
$tags = wp_get_object_terms( $post->ID, 'TAGS-3' );
if ( $tags ) : ?>
<ul id="TAGS-LIST">
<?php foreach ( $tags as $tag ) : ?>
<li>
<?php $tag_link = esc_url( get_term_link( $tag ) ); ?>
<a href="<?php echo $tag_link; ?>" class="TAG-CLASS">
<span class="TAG-TEXT-CLASS"><?php echo $tag->name; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul><!-- #TAG-LIST -->
<?php endif; ?>
<?php } ?>
</div>
</div>https://stackoverflow.com/questions/48352739
复制相似问题