Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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 Woocommerce-在类别页面上显示不同的产品缩略图_Php_Wordpress_Woocommerce_Hook - Fatal编程技术网

Php Woocommerce-在类别页面上显示不同的产品缩略图

Php Woocommerce-在类别页面上显示不同的产品缩略图,php,wordpress,woocommerce,hook,Php,Wordpress,Woocommerce,Hook,我想为我的一些产品显示不同的缩略图,如果它们出现在特定类别中 我已经从特定类别中删除了当前的缩略图,我正在使用一个自定义字段为我要替换的产品提取新的缩略图,这非常有效。然而,当我尝试调用剩余产品的正常缩略图时,它不起作用-有什么想法吗 add_action( 'woocommerce_before_main_content', 'remove_test_category_thumbnails', 10 ); function remove_test_category_thumbnails()

我想为我的一些产品显示不同的缩略图,如果它们出现在特定类别中

我已经从特定类别中删除了当前的缩略图,我正在使用一个自定义字段为我要替换的产品提取新的缩略图,这非常有效。然而,当我尝试调用剩余产品的正常缩略图时,它不起作用-有什么想法吗

add_action( 'woocommerce_before_main_content', 'remove_test_category_thumbnails', 10 );

function remove_test_category_thumbnails() {
    if (is_product_category('test-category')) {
        remove_action('woocommerce_before_shop_loop_item_title' , 'woocommerce_template_loop_product_thumbnail' , 10);
    }
}

add_action( 'woocommerce_before_shop_loop_item_title', 'add_test_category_thumbnails', 10 );

function add_test_category_thumbnails() {
    global $post;
    $testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true);
    if (is_product_category('test-category') && (isset($testCatThumb)) ) {
        echo wp_get_attachment_image( $testCatThumb );
    }
    else 
        echo woocommerce_get_product_thumbnail();
    }
}

您需要
echo
查看
woocommerce\u get\u product\u缩略图
。检查下面的代码

add_action( 'woocommerce_before_main_content', 'remove_test_category_thumbnails', 10 );
function remove_test_category_thumbnails() {
    if (is_product_category('test-category')) {
        remove_action('woocommerce_before_shop_loop_item_title' , 'woocommerce_template_loop_product_thumbnail' , 10);
    }
}

add_action( 'woocommerce_before_shop_loop_item_title', 'add_test_category_thumbnails', 10 );
function add_test_category_thumbnails() {
    global $post;
    $testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true); 
    if( is_product_category( 'test-category' ) && ( isset( $testCatThumb ) ) ) {
        echo wp_get_attachment_image( $testCatThumb );
    }else{
        echo woocommerce_get_product_thumbnail();
    } 
}

下面的函数需要一个post id,这就是您要传递的吗

wp_get_attachment_image();
作为if条件的一部分,您可以检查值是否为正确的值类型:

is_numeric($testCatThumb);
我倾向于创建可读的变量,而不是每次阅读时都必须用大脑分析的可怕条件:

function add_test_category_thumbnails() {
    global $post;

    $testCatThumb        = get_post_meta( $post->ID, 'step_dad_mug', true);
    $isPostId            = !empty($testCatThumb) && is_numeric($testCatThumb);
    $isValidAttachmentId = is_product_category('test-category') && $isPostId;
    $image               = $isValidAttachmentId ? wp_get_attachment_image($testCatThumb) : '';

    // display custom image, or fallback to woocommerce thumbnail
    echo !empty($image) ? $image : woocommerce_get_product_thumbnail();
}

谢谢你的回复。我一定错过了这一点,因为我一直在尝试许多不同的变体来尝试并使其工作,但是,添加echo并不能解决它。我测试并为我工作得很好。我在我的网站上测试了它,它显示了带有自定义字段的产品上的新缩略图(正确)但是没有自定义字段的产品不会显示产品缩略图。
isset()
单独检查是不够的。如果
$testCatThumb
变量返回空值,
isset($testCatThumb)
将返回true,因为它只检查变量是否声明且不为NULL。在您的情况下,还应添加
!空($testCatThumb)
。因此,if语句可以是:
if(is_product_category('test-category')&&&(isset($testCatThumb)&&&!empty($testCatThumb))){
。感谢您的回复。我尝试了您的代码,它似乎显示了原始的缩略图,但不是我要替换的缩略图。有什么想法吗?有两种选择:您没有在产品类别页面中显示
测试类别
slug,或者该产品没有
step\u dad\u mug
自定义post meta。