WP - Woo - 基于类别选择的条件定制产品标签

时间:2018-01-20 04:54:53

标签: php wordpress if-statement woocommerce hook-woocommerce

我有3个产品类别 - CATEGORY-1,CATEGORY-2和CATEGOTRY-3

我还创建了3个额外的自定义分类,用作每个类别的TAGS,分别是TAGS-1,TAGS-2和TAGS-3。

我编辑了Woocommerce单品并创建了自定义标签。在此选项卡中,我想根据类别选择显示我的自定义标签。

因此,如果为产品选择了CATEGORY-1,那么我想显示TAGS-1,如果选择了CATEGORY-2,那么我想显示TAGS-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; ?>

但不幸的是,我似乎无法使其发挥作用。根本没有输出显示。我哪里错了?

2 个答案:

答案 0 :(得分:0)

在不知道产品数据的情况下,很难确切地说出原因。我会在那里放一些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'; }  ?>

这有帮助吗?

答案 1 :(得分:0)

好的,所以我走了很长的路,虽然现在工作正常,有没有办法让整个事情变得更短?

<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>