将文本标签更改为图像

时间:2016-11-01 作者:Rangga Setiaga

我想将文本标签更改为图像。此标签位于WooCommerce商店页面。

Member Discount

脚本包含文本“会员折扣!”:

$label = __( \'Member discount!\', \'woocommerce-memberships\' );
我尝试更改脚本,例如:<img src="http://siteh87h8.com/gambar.png" />, 但文本仍然没有更改。

相关代码:

/**
 * Get member discount badge
 *
 * @since 1.6.4
 * @param \\WC_Product $product The product object to output a badge for (passed to filter)
 * @param bool $variation Whether to output a discount badge specific for a product variation (default false)
 * @return string
 */
public function get_member_discount_badge( $product, $variation = false ) {

      $label = __( \'Member discount!\', \'woocommerce-memberships\' );
    // we have a slight different output for badge classes and filter
    if ( true !== $variation ) {
        global $post;

        // used in filter for backwards compatibility reasons
        $the_post = $post;

        if ( ! $the_post instanceof WP_Post ) {
            $the_post = $product->post;
        }

        $badge = \'<span class="onsale wc-memberships-member-discount">\' . esc_html( $label ) . \'</span>\';

        /**
         * Filter the member discount badge
         *
         * @since 1.0.0
         * @param string $badge The badge HTML
         * @param \\WP_Post $post The product post object
         * @param \\WC_Product_Variation $variation The product variation
         */
        $badge = apply_filters( \'wc_memberships_member_discount_badge\', $badge, $the_post, $product );

    } else {

        $badge = \'<span class="wc-memberships-variation-member-discount">\' . esc_html( $label ) . \'</span>\';

        /**
         * Filter the variation member discount badge
         *
         * @since 1.3.2
         * @param string $badge The badge HTML
         * @param \\WC_Product|\\WC_Product_Variation $variation The product variation
         */
        $badge = apply_filters( \'wc_memberships_variation_member_discount_badge\', $badge, $product );

    }

    return $badge;
}


/**
 * Filter the member discount badge for products excluded
 * from member discount rules
 *
 * @internal
 *
 * @since 1.7.0
 * @param string $badge Badge HTML
 * @param \\WP_Post $post The post object
 * @param \\WC_Product $product The product object
 * @return string Empty string if product is excluded from member discounts
 */
public function disable_discount_badge_for_excluded_products( $badge, $post, $product ) {
    return $this->is_product_excluded_from_member_discounts( $product ) ? \'\' : $badge;
}
此处为完整脚本:http://pastebin.com/AGztQYYt (相关代码从第1032行开始)。

Edit: 这个公认的答案对我很有用。除了答案中的代码外,我还使用了以下CSS:

.woocommerce ul.products.grid li.product .badgescarisan{position:absolute;right: 0px;}
还有这个HTML:

$badge = \'<span class="badgescarisan wc-memberships-member-discount">\' .
                        \'<img src="https://www.conecplus.com/wp-content/uploads/2016/11/Badges-Membership-Carisan.png" alt="meow">\' .
                \'</span>\';

2 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

更改标签文本(仅限文本)

文本“会员折扣!”可以使用翻译过滤器进行更改,但无法向其中添加图像或任何HTML,因为稍后在问题中引用的代码中会对其进行转义:

 $badge = \'<span class="onsale wc-memberships-member-discount">\' . esc_html( $label ) . \'</span>\';
下面是一个示例,演示如何更改会员折扣文本,但它仅适用于文本,而不适用于HTML:

add_filter( \'gettext\', \'wpse244685_change_text\', 20, 3 );
function wpse244685_change_text( $translated_text, $untranslated_text, $domain ) {
    if ( \'woocommerce-memberships\' !== $domain ) {
        return $translated_text;        
    }

    // make the changes to the text
    switch( $untranslated_text ) {

            case \'Member discount!\' :
                // $translated_text = __( \'New text\', \'text_domain\' ); // Example of new string
                $translated_text = \'\'; // Empty strings should not be translated
            break;

            // add more items
     }

    return $translated_text;        
}
添加图像或其他HTML取决于产品的配置方式,您可以通过wc_memberships_member_discount_badgewc_memberships_variation_member_discount_badge 过滤器如下所示:

Standard Product:

add_filter( \'wc_memberships_member_discount_badge\', \'wpse244685_wc_memberships_member_discount_badge\', 10, 3 );
function wpse244685_wc_memberships_member_discount_badge( $badge, $the_post, $product ) {
    $badge = \'<span class="onsale wc-memberships-member-discount">\' . 
                            \'<img src="https://placekitten.com/300/300" alt="meow">\' .
                     \'</span>\';

    return $badge;      
}

Variable Product:

add_filter( \'wc_memberships_variation_member_discount_badge\', \'wpse244685_wc_memberships_variation_member_discount_badge\', 10, 2 );
function wpse244685_wc_memberships_member_discount_badge( $badge, $product ) {
    $badge = \'<span class="onsale wc-memberships-member-discount">\' . 
                            \'<img src="https://placekitten.com/300/300" alt="meow">\' .
                     \'</span>\';

    return $badge;      
}

SO网友:Rangga Setiaga

成功^o^I将css类“onsale”更改为“badgescarisan”

.woocommerce ul.products.grid li.product .badgescarisan{position:absolute;right: 0px;}
并添加图片

$badge = \'<span class="badgescarisan wc-memberships-member-discount">\' . \'<img src="https://www.conecplus.com/wp-content/uploads/2016/11/Badges-Membership-Carisan.png" alt="meow">\' .\'</span>\';
谢谢你帮助我。对不起,我的英语不好^_^

相关推荐

Changing Admin Menu Labels

我用了最后一天的时间使用这些函数。php文件为我的客户端站点完全定制WordPress。我很惊讶我能完成这么多,也很惊讶它能让我的客户更轻松。我已删除了未以管理员身份登录的用户的某些菜单项。我希望(从我所读到的内容来看,我知道这是可以做到的)找到一种方法来重命名一些菜单项(管理区域的左侧边栏)。例如,将帖子更改为文章。如果有人能提供函数的代码。php文件或指向我的方向,我将不胜感激!