【问题标题】:Change text from search box when no product found未找到产品时更改搜索框中的文本
【发布时间】:2021-01-09 02:54:11
【问题描述】:

我正在使用 WooCommerce 开发一个 WordPress 网站,搜索框显示文本“未找到产品”。产品不可用时的文本。

如何将此文本更改为其他内容“别担心,稍后再试”

我不知道如何处理这个问题,因为我对这种修改很陌生。

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce


    【解决方案1】:
    add_filter( 'woocommerce_register_post_type_product', 'woocommerce_register_post_type_product' );
    
    function woocommerce_register_post_type_product( $labels ) {
        $labels[ 'labels' ][ 'not_found' ] = __( 'don\'t worry, try again later', 'woocommerce' );
        return $labels;
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用gettext 挂钩来更改文本。

      把它放在你的functions.php中 使用开关,你只会影响“woocommerce”文本域中的文本。

      function dd_change_text_strings( $translated_text, $text, $domain ) {
          switch ( $translated_text ) {
              case 'No products found' :
                  if ($domain == 'woocommerce'){
                      $translated_text = __( "Don\'t worry try again later", 'woocommerce' );
                  } else {
                      $translated_text = __( 'No products found', $domain );
                  }
                  break;
          }
          return $translated_text;
      }
      add_filter( 'gettext', 'dd_change_text_strings', 20, 3 );
      

      【讨论】:

      • 我什至更改了 $domain 的“未找到产品”,但它仍然不起作用。我也清除了缓存。
      • 这有效 $translated = str_replace( 'No products found', 'New Message', $translated );
      猜你喜欢
      • 2018-02-13
      • 2014-12-31
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多