更改或翻译Woocommerce中的特定文本

Mik*_*ung 4 php wordpress translation gettext woocommerce

我在网上找到了一个解决方案,但似乎没有用.

它说要编辑我几天前做过的下面的文件,但不知何故它还没有工作.

/wp-content/plugins/woocommerce/templates/single-product/related.php

如果我FTP到服务器,该文件显示如下:

if ( $products->have_posts() ) : ?>

<div class="related products">

    <h2><?php _e('You may also like', 'woocommerce' ); ?></h2>
Run Code Online (Sandbox Code Playgroud)

然而,该网页仍显示"相关产品",而不是"您可能也喜欢"

出于某种原因,这不是在某个地方发生或过度骑行.

有任何想法吗?

小智 22

我为孩子发现了这个函数.php:http://speakinginbytes.com/2013/10/gettext-filter-wordpress/

/**
 * Change text strings
 *
 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
 */
function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Related Products' :
            $translated_text = __( 'Check out these related products', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
Run Code Online (Sandbox Code Playgroud)

它运作良好:https://mo-sound.com/en/shop/ball-speaker-classic-white/

  • 这是正确的解决方案,功能覆盖.不是主题更改,因为主题文件也可能会更新. (4认同)

RST*_*RST 8

覆盖默认模板的最佳方法是将文件复制到/woocommerce/single-product当前主题中指定的文件夹中.对该文件进行更改.

一般来说,覆盖Woocommerce模板文件,如

/wp-content/plugins/woocommerce/templates/<foldername>/<filename>

你将文件复制到

/wp-content/<your-theme>/woocommerce/<foldername>/<filename>


Mar*_*han 5

这是user5217948带有所需案例更新的代码Frithir

// CHANGE RELATED PRODUCTS TEXT
function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Related products' :
            $translated_text = __( 'You may also like...', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
Run Code Online (Sandbox Code Playgroud)

此代码适用于 WooCommerce 3.3.1 和 WordPress 4.9.4(大约在 2018 年 2 月)。


.


Mar*_*nni 5

对于那些使用另一种语言的 woocommerce/wordpress 的人的友好提示

您必须将“相关产品”替换为以您的语言显示的文本。就我而言,相关产品翻译为“productos relacionados”

包含在functions.php文件中的代码

function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
    case 'Productos relacionados' :
        $translated_text = __( 'Completá el look', 'woocommerce' );
        break;
}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
Run Code Online (Sandbox Code Playgroud)