Woocommerce显示产品的类别标题和图像

时间:2015-11-13 12:25:13

标签: php wordpress plugins woocommerce

在Wordpress Woocommerce中如何在Storefront主题中正确显示产品的类别标题和图像?

3 个答案:

答案 0 :(得分:0)

您首先需要获取类别ID:

global $post;    
$terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ($terms as $term) {
        $product_cat_id = $term->term_id;
        break;
    }

接下来,当你拥有它时 - 尝试获取当前类别的缩略图ID:

// get the thumbnail id user the term_id
        $thumbnail_id = get_woocommerce_term_meta( $product_cat_id, 'thumbnail_id', true ); 

最后 - 获取图片:

// get the image URL
        $image = wp_get_attachment_url( $thumbnail_id ); 
echo '<img src="'.$image.'" alt="" />';

我希望它可以帮到你。

答案 1 :(得分:0)

这是模板代码:

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <?php while ( have_posts() ) : the_post(); ?>

            <?php
            do_action( 'storefront_page_before' );
            ?>

            <?php get_template_part( 'content', 'page' ); ?>

            <?php
            /**
             * @hooked storefront_display_comments - 10
             */
            do_action( 'storefront_page_after' );
            ?>

        <?php endwhile; // end of the loop. ?>

    </main><!-- #main -->
</div><!-- #primary -->

答案 2 :(得分:0)

感谢您的回复,这里是主题的content-page.php:

<?php
    /**
     * The template used for displaying page content in page.php
     *
     * @package storefront
     */
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <?php
        /**
         * @hooked storefront_page_header - 10
         * @hooked storefront_page_content - 20
         */
        do_action( 'storefront_page' );
    ?>
</article><!-- #post-## -->

这是woocommerece插件的content-product.php

<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $product, $woocommerce_loop;

// Store loop count we're currently on
if ( empty( $woocommerce_loop['loop'] ) ) {
    $woocommerce_loop['loop'] = 0;
}

// Store column count for displaying the grid
if ( empty( $woocommerce_loop['columns'] ) ) {
    $woocommerce_loop['columns'] = apply_filters( 'loop_shop_columns', 4 );
}

// Ensure visibility
if ( ! $product || ! $product->is_visible() ) {
    return;
}

// Increase loop count
$woocommerce_loop['loop']++;

// Extra post classes
$classes = array();
if ( 0 == ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 == $woocommerce_loop['columns'] ) {
    $classes[] = 'first';
}

if ( 0 == $woocommerce_loop['loop'] % $woocommerce_loop['columns'] ) {
    $classes[] = 'last';
}
?>
<li <?php post_class( $classes ); ?>>
    <?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
    <a href="<?php the_permalink(); ?>">
    <?php
        /**
         * woocommerce_before_shop_loop_item_title hook
         *
         * @hooked woocommerce_show_product_loop_sale_flash - 10
         * @hooked woocommerce_template_loop_product_thumbnail - 10
         */
        do_action( 'woocommerce_before_shop_loop_item_title' );

        /**
         * woocommerce_shop_loop_item_title hook
         *
         * @hooked woocommerce_template_loop_product_title - 10
         */
        do_action( 'woocommerce_shop_loop_item_title' );

        /**
         * woocommerce_after_shop_loop_item_title hook
         *
         * @hooked woocommerce_template_loop_rating - 5
         * @hooked woocommerce_template_loop_price - 10
         */
        do_action( 'woocommerce_after_shop_loop_item_title' );
    ?>
   </a>
   <?php
       /**
        * woocommerce_after_shop_loop_item hook
        *
        * @hooked woocommerce_template_loop_add_to_cart - 10
        */
       do_action( 'woocommerce_after_shop_loop_item' );
   ?>
</li>