向 Woocommerce 产品块添加 figcaption 会复制产品吗?

问题描述

我正在尝试使用“woocommerce_blocks_product_grid_item_html”过滤器向产品块添加 figcaption,在标题之前和购买按钮之后打开。

我似乎设法让 figcaption 标签显示在正确的位置,但是当我这样做时,产品被复制了。如何使用 substr_replace 函数在产品块中添加标签

下面的代码...我是 wordpress 主题开发的新手。

/**
     * Add figcaption to WooCommerce product blocks
     */
    add_filter('woocommerce_blocks_product_grid_item_html','add_short_desc_woocommerce_blocks_product_grid_item_html',10,3);
    function add_short_desc_woocommerce_blocks_product_grid_item_html($content,$data,$product) {

        
        //Insert figcaption
        $open_fig = '<figcaption>';
        $close_fig = '</figcaption>';

        // Find the places in the string 
        $before_link_pos = strpos($content,"</a>");
        $after_pos = strrpos($content,"<div");
        // Echo figcaption variables in place 
        $figcaption1= substr_replace($content,$open_fig,$before_link_pos,0); 
        $figcaption2= substr_replace($content,$close_fig,$after_pos,0);
        
        $content =  $figcaption1 . $figcaption2;
        return $content;
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)