亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 WooCommerce 產品描述中顯示所有圖像

如何在 WooCommerce 產品描述中顯示所有圖像

PHP
侃侃爾雅 2023-07-01 17:46:48
我想用單個產品頁面中的描述+所有產品圖片(包括變體產品圖片)替換我的產品描述。我可以使用 Magento 做到這一點,但現在當更改為 Woocommerce 時我不能。經過研究,我嘗試使用過濾鉤,但沒有成功。我可以將文本添加到產品描述中,但我一直在思考如何使用函數wp_get_attachment_image_url()或wp_get_attachment_image().據我所知,顯示圖像的示例代碼:echo wp_get_attachment_image( get_the_ID(), array('700', '600'), "", array( "class" => "img-responsive" ) ). 如何在我的代碼中應用?    // Display description tab when empty    add_filter( 'woocommerce_product_tabs', 'display_description_product_tabs' );        function display_description_product_tabs( $tabs ) {        $tabs['description'] = array(            'title'    => __( 'Description', 'woocommerce' ),            'priority' => 10,            'callback' => 'woocommerce_product_description_tab',        );        return $tabs;    }    // Add image to description    add_filter( 'the_content', 'add_product_image_woocommerce_description' );    function add_product_image_woocommerce_description( $content ) {        global $product;        // Single product pages (woocommerce)        if ( is_product() ) {            // Image id            $attachment_ids = $product->get_gallery_image_ids();            $image_content = "";            foreach( $attachment_ids as $attachment_id ) {                $image_content .= '<img src="'. wp_get_attachment_image_url($attachment_id, 'full') . '" alt="image_content" width="500" height="600">';            }            $image_content .= '<p> TEST Image content </p>';            // Inserting the custom content at the end            $content .= $image_content;        }        return $content;    }
查看完整描述

1 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

您可以使用如下WC_Product?get_image()方法:

echo?$product->get_image(?array('700',?'600'),?array(?"class"?=>?"img-responsive"?),?''?);

然后,在您的代碼中,您可以緩沖所有回顯的自定義代碼以及主產品圖像(在產品描述內容的末尾),如下所示:

// Display description tab when empty

add_filter( 'woocommerce_product_tabs', 'display_description_product_tabs' );

? ? function display_description_product_tabs( $tabs ) {


? ? $tabs['description'] = array(

? ? ? ? 'title'? ? => __( 'Description', 'woocommerce' ),

? ? ? ? 'priority' => 10,

? ? ? ? 'callback' => 'woocommerce_product_description_tab',

? ? );


? ? return $tabs;

}


// Add image to description

add_filter( 'the_content', 'add_product_image_woocommerce_description' );

function add_product_image_woocommerce_description( $content ) {

? ? global $product;


? ? // Single product pages (woocommerce)

? ? if ( is_product() ) {


? ? ? ? ob_start(); // Start buffering


? ? ? ? // HERE your main image

? ? ? ? echo $product->get_image( array('700', '600'), array( "class" => "img-responsive" ), '' );


? ? ? ? $image_content = "";


? ? ? ? // Loop through gallery Image Ids

? ? ? ? foreach( $product->get_gallery_image_ids() as $image_id ) {


? ? ? ? ? ? echo '<img src="'. wp_get_attachment_image_url($image_id, 'full') . '" alt="image_content" width="500" height="600">';

? ? ? ? }


? ? ? ? echo '<p> TEST Image content </p>'; // Testing text


? ? ? ? // Inserting the buffered content after

? ? ? ? $content .= ob_get_clean();

? ? }


? ? return $content;

}


查看完整回答
反對 回復 2023-07-01
  • 1 回答
  • 0 關注
  • 184 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號