更改星级评分旁边的文本的方式如何。

huangapple go评论52阅读模式
英文:

Woocommerce - How to change the text next to star rating

问题

您想要修改以下HTML中的文本“customer reviews”:

<a href="#reviews" class="woocommerce-review-link" rel="nofollow"><span class="count">1</span> customer reviews</a>

您尝试了以下代码,但它对您不起作用:

add_filter( 'gettext', function( $translated_text ) {
	if ( 'customer reviews' === $translated_text ) {
		$translated_text = 'reviews';
	}
	return $translated_text;
} );

这段代码的目的是将“customer reviews”翻译为“reviews”。

英文:

Thank for reading my issue.
Please help that which code snippets can let me change the text "customer reviews" in html below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;a href=&quot;#reviews&quot; class=&quot;woocommerce-review-link&quot; rel=&quot;nofollow&quot;&gt;&lt;span class=&quot;count&quot;&gt;1&lt;/span&gt; customer reviews&lt;/a&gt;

<!-- end snippet -->

I had try this but it not work for me.

add_filter( &#39;gettext&#39;, function( $translated_text ) {
	if ( &#39;**customer reviews**&#39; === $translated_text ) {
		$translated_text = &#39;**reviews**&#39;;
	}
	return $translated_text;
} );

Thank you!

答案1

得分: 0

这是一个有点复杂的情况,我将尽量使用简单的英语。如果您通过WooCommerce插件搜索所需的字符串,您将找到以下内容:

_n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' )

这意味着您不能使用gettext过滤器,而是需要使用ngettext,因为这是一个更复杂的国际化函数:

add_filter( 'ngettext', 'bbloomer_modify_n_customer_reviews', 9999, 5 );

function bbloomer_modify_n_customer_reviews( $translation, $single, $plural, $number, $domain ) {
    $translation = preg_replace( '%s customer reviews', '%s reviews', $translation );
    return $translation;
}
英文:

This is a little complex so I'll try to use plain English where possible. If you do a search through the WooCommerce plugin for the string you need, you'll find this:

_n( &#39;%s customer review&#39;, &#39;%s customer reviews&#39;, $review_count, &#39;woocommerce&#39; )

...which means you can't use the gettext filter, you need the ngettext one instead as this is a more complex internationalization function:

add_filter( &#39;ngettext&#39;, &#39;bbloomer_modify_n_customer_reviews&#39;, 9999, 5 );

function bbloomer_modify_n_customer_reviews( $translation, $single, $plural, $number, $domain ) {
    $translation = preg_replace( &#39;%s customer reviews&#39;, &#39;%s reviews&#39;, $translation );
    return $translation;
}

答案2

得分: 0

在WordPress中,要更改前端或后台显示的任何文本,您可以使用以下代码来进行更改:

add_filter('gettext', '_translateTextCustom', 100, 3 );
function _translateTextCustom( $translated_text, $text, $domain ) {
  global $pagenow, $post_type;

  if ( !is_admin() && "customer reviews" === $text && 'woocommerce' === $domain )
  {
    $translated_text =  __('reviews', $domain );
  }
  return $translated_text;
}
  1. global $pagenow - 使用这个全局变量,您可以访问所有的php页面。

> in_array( $pagenow, ['post.php', 'post-new.php']

  1. global $post_type - 使用这个全局变量,您可以有条件地访问所有的文章类型。

> 'product' === $post_type || 'page' === $post_type || 'post' === $post_type

英文:

In WordPress for changing any text showing on front end or on admin you can change with add_filter(&#39;gettext&#39;, &#39;_translateTextCustom&#39;, 100, 3 );

Try below code snippet

add_filter(&#39;gettext&#39;, &#39;_translateTextCustom&#39;, 999, 3 );
function _translateTextCustom( $translated_text, $text, $domain ) {
  global $pagenow, $post_type;

  if ( !is_admin() &amp;&amp; &quot;customer reviews&quot; === $text &amp;&amp; &#39;woocommerce&#39; === $domain )
  {
    $translated_text =  __( &#39;reviews&#39;, $domain );
  }
  return $translated_text;
}
  1. global $pagenow - You will access all php pages with this global variable.

> in_array( $pagenow, [&#39;post.php&#39;, &#39;post-new.php&#39;]

  1. global $post_type - You will access all post types which you want conditionally.

> &#39;product&#39; === $post_type || &#39;page&#39; === $post_type || &#39;post&#39; === $post_type

huangapple
  • 本文由 发表于 2023年4月13日 19:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定