Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用不同大小的类别图像_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 如何使用不同大小的类别图像

Php 如何使用不同大小的类别图像,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,希望有人能帮忙 我正在建立一个WooCommerce网站,我遇到了分类图片的问题 当你点击商店时,会看到一个包含各种类别缩略图的页面。当你点击缩略图时,你会进入一个页面,页面上有与该类别相关的产品 我要寻找的是,然后使用相同的类别图像从上一页作为标题。问题是图像的大小与预期的不同,即全尺寸 我在functions.php中规定了一个名为feat img的大小,但我似乎不知道如何使用它 下面是我用来显示类别图像的代码: <?php if (is_product_cat

希望有人能帮忙

我正在建立一个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”尺寸,如有任何帮助,将不胜感激


谢谢

首先像这样在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(); ?>

“alt=”“>

首先像这样在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(); ?>

“alt=”“>