【问题标题】:Add image caption under the first image in a WooCommerce single product page在 WooCommerce 单品页面的第一张图片下添加图片标题
【发布时间】:2020-11-13 15:47:49
【问题描述】:

使用 Add image caption under image thumbnail in WooCommerce single product page 答案的代码,我想做同样的事情,但只针对第一张图片而不是所有缩略图。

我该怎么做?

【问题讨论】:

  • 请检查我的回答。

标签: javascript wordpress woocommerce caption


【解决方案1】:

请尝试以下钩子,

add_filter('woocommerce_single_product_image_thumbnail_html', 'thumb_add_caption', 10, 4 );
function thumb_add_caption( $html, $id, $parent, $class) {
   $attachment = get_post( $id );
   $img_cap = "<div class=\"img-cap\"><span class=\"cap-text\">{$attachment->post_excerpt}</span></div>$html";
   return $img_cap;
}

function add_caption_below_product_image() {

    echo '<h4 style="text-align: center;">';
    $caption = get_post( get_post_thumbnail_id() )->post_excerpt;
    echo $caption;
    echo '</h4>';
}
add_action( 'woocommerce_product_thumbnails', 'add_caption_below_product_image' );

【讨论】:

  • 第一个代码破坏了我的网站第二个不起作用我使用 Divi 模板标题的属性是 data-caption
【解决方案2】:

我有这个代码的东西

jQuery(document).ready(function($) {
$('.woocommerce-product-gallery__wrapper').each(function(){
    $('.woocommerce-product-gallery__image', this).after('<div class="legende_image"></div>');
    $('.legende_image', this).html($('img', this).attr('data-caption'));
});   });

还有这个css代码

.woocommerce-product-gallery__wrapper {
position: relative;}

.legende_image {
position: absolute;
z-index: 1;
padding: 10px;
background-color: #000;
font-style: italic;
line-height: 1em;
font-size: 14px;
color: #fff;
bottom:0;
white-space: pre-line;}

但标题保留在我的图像顶部 你有想法吗 ? 谢谢

【讨论】:

    猜你喜欢
    • 2019-07-18
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2020-10-23
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    相关资源
    最近更新 更多