【问题标题】:How To Use a Different sized Category Image如何使用不同大小的类别图像
【发布时间】:2018-01-23 06:22:24
【问题描述】:

希望有人能提供帮助。

我正在构建 WooCommerce 网站,但遇到了类别图片问题。

当您点击商店时,会出现一个包含各种类别缩略图的页面。当您单击缩略图时,您将被带到与该类别相关的产品页面。

我正在寻找的是然后使用上一页中的相同类别图像作为标题。问题是图像的预期尺寸不同,即全尺寸。

我在functions.php 中规定了一个名为feat-img 的大小,但我似乎不知道如何使用它。

这是我用来显示类别图像的代码:

<?php
            if (is_product_category()){
                global $wp_query;
                $cat = $wp_query->get_queried_object();
                $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); 
                $image = wp_get_attachment_url( $thumbnail_id ); 
                echo '<img class="img-responsive" src="'.$image.'" alt="" />';
            }
        ?>

任何关于我如何使用“feat-img”大小的帮助都将不胜感激。

谢谢

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    首先在functions.php中这样定义

    add_image_size('300pxSize',300,300);
    add_image_size( 'full-width-ratio', 600, 9999 );
    add_image_size( 'full-width-crop', 600, 300, true );
    add_image_size( 'full-width-crop-h200', 600, 200, true );
    
    //get featured image
    function f_image($id,$width="300pxSize") {
        $postThumbnailId = get_post_thumbnail_id( $id );
        $imgsrc = wp_get_attachment_image_src( $postThumbnailId, $width);
    
        return  $imgsrc[0];
    }
    // Check if post has image
    function hasFeaturedImage($id) {
        $fImg = f_image($id);
        if($fImg)
        {
            return true;
        }
        return false;
    }
    

    然后像这样调用你的页面(例如single.php):

      <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
        <?php if(hasFeaturedImage(get_the_ID())) : ?>
          <div class="single-post-image">
            <img src="<?php echo f_image(get_the_ID(),"full-width-crop"); ?>" alt="">
          </div>
        <?php endif; ?>
      <?php endwhile;endif;wp_reset_query(); ?>
    

    【讨论】:

      猜你喜欢
      • 2014-11-27
      • 2018-04-14
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      相关资源
      最近更新 更多