wordpress 产品循环标签woocommerce

ql3eal8s  于 2022-12-26  发布在  WordPress
关注(0)|答案(2)|浏览(154)

我试图在循环中添加一个get_tags,但它似乎不起作用。

<?php
            $args = array ( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 3, 'product_cat' => 'kuhinja', 'orderby' => 'date','order' => 'DESC' );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

                    <li class="span3">    

                        <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

                            <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>

                            <h3><?php the_title(); ?></h3>
                            <h3><?php the_tags() ?></h3>
                               <span class="price"><?php echo $product->get_price_html();?>/ po komadu</span>

                        </a>

                        <p class="u-kosaricu"><?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?></p>
                    </li><!-- /span3 -->
        <?php endwhile; ?>
        <?php wp_reset_query(); ?>

我做错什么了?谢谢

mqkwyuun

mqkwyuun1#

嗨试试这个...

$args = array ( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 3, 'product_cat' => 'kuhinja', 'orderby' => 'date','order' => 'DESC' );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

                <li class="span3">    

                    <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

                        <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
                        <?php 
                        $terms = get_the_terms( $post->ID, 'product_tag' ); 
                        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ):
                          foreach ( $terms as $term ) :
                            $slug_name = $term->slug;

                          endforeach;
                        endif;  

                        ?>
                        <h3><?php the_title(); ?></h3>
                        <h3><?php echo $slug_name; ?></h3>
                           <span class="price"><?php echo $product->get_price_html();?>/ po komadu</span>

                    </a>

                    <p class="u-kosaricu"><?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?></p>
                </li><!-- /span3 -->
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
ercv8c1e

ercv8c1e2#

试试这个代码

add_action( 'woocommerce_before_shop_loop_item_title', 'show_tags', 20   );
function show_tags() {
  global $product;
  // get the product_tags of the current product
  $current_tags = get_the_terms( get_the_ID(), 'product_tag' );
  // only start if we have some tags
  if ( $current_tags && ! is_wp_error( $current_tags ) ) { 
    //create a list to hold our tags
    echo '<ul class="product_tags">';
    // for each tag we create a list item
    foreach ( $current_tags as $tag ) {
      $tag_title = $tag->name; // tag name  
      echo '<p class="loop-tags">'.$tag_title.'</p>';
    }
    echo '</ul>';
  }
}

相关问题